Diamond problem

  • Thread starter Thread starter weird0
  • Start date Start date
W

weird0

How does C++ and C# solve the Diamond problem? With the help of
interfaces that is.

Can anyone elaborate .......

Regards
 
weird0 said:
How does C++ and C# solve the Diamond problem? With the help of
interfaces that is.

C# does not support multiple inheritance, therefore the diamond problem does
not occur. Can't tell you what rules C++ uses to resolve this ambiguity but
I suspect it requires you to be explicit.

Where in C# you may implement multiple interfaces where a similar ambiguity
may arise you are required to be explicit, the compilier won't guess.
 
weird0 said:
How does C++ and C# solve the Diamond problem? With the help of
interfaces that is.

Can anyone elaborate .......

Regards


What is the diamond problem?
 
Base Class A
Classes B and C derived from A
Class D derived from B and C (mulitple inheritence).

I should of added the diamond problem is where B and C both have a
method or property with the same name. What happens in D?
I'm having a bad day, and google groups is playing up now too *rant*
 
DeveloperX said:
I should of added the diamond problem is where B and C both have a
method or property with the same name. What happens in D?
I'm having a bad day, and google groups is playing up now too *rant*


But as has been said, C# doesn't have multiple inheritance. I think your
lecturer is asking you a trick question <g>

C# does support multiple interface implementation and has both implicit and
explicit interfaces. Explicit interfaces don't suffer from member name
clashes.
 
But as has been said, C# doesn't have multiple inheritance.  I think your
lecturer is asking you a trick question <g>

C# does support multiple interface implementation and has both implicit and
explicit interfaces. Explicit interfaces don't suffer from member name
clashes.- Hide quoted text -

- Show quoted text -

Yep, Anthony explained that, I was just answering the question, "what
is the diamond problem" :)
 
DeveloperX said:
I should of added the diamond problem is where B and C both have a
method or property with the same name. What happens in D?
I'm having a bad day, and google groups is playing up now too *rant*


Thank yo for your explanation. I can see where that situation might pose a
problem.

Todd
 
The Diamond solution was going to be explicit declarations where the derived
class would say which base class members would be unified and which would be
separated as separate members. Also it would have allowed optional renaming
in inheritance, so that maintenance programmers could decide for themselves
how to choose the lesser of two maintenance nightmares every time it came
up. But that was 19 years ago, it wasn't for C++ or C#, and my
then-employer had lied when they said they wanted me to work on this kind of
thing.
 
How does C++ and C# solve the Diamond problem? With the help of
interfaces that is.

As others have pointed out, the diamond problem doesn't exist in C#
because there's no multiple inheritance (except with interfaces, which
are unaffected by this problem).

It exists in C++ because C++ does have multiple inheritance for
classes. For example, say you have a base class A which defines the
member "foo", two classes B and C which inherit from A and define the
members "bar" and "baz" respectively, and finally a derived class D
which derives from both B and C.

Now, the memory layout for a derived class normally consists of its
base class's layout plus ever new members are defined:

A's layout:
int foo

B's layout:
// copied from A
int foo
// introduced in B
int bar

C's layout:
// copied from A
int foo
// introduced in C
int baz

But then what does D look like? Well, it looks like both of its base
classes put together:
// copied from B
int foo
int bar
// copied from C
int foo
int baz

Notice that there are two "foo" members: that's the diamond problem.
Foo needs to be duplicated because the rules of inheritance say that a
D instance can be cast to an instance of either B or C (its bases).
That means its memory layout must contain a full instance of B and a
full instance of C. And that means that the methods D inherits from B
will see a *different* foo than the methods it inherits from C.

C++ solves the problem by introducing "virtual" base classes, which
don't have to appear in a fixed location in their derived classes'
memory layouts. By defining A as a *virtual* base of B and C, the
memory layouts might look like this instead:

B's layout II:
// copied from A
int foo
// introduced in B
<pointer to A>
int bar

C's layout II:
// copied from A
int foo
// introduced in C
<pointer to A>
int baz

Now when you define D as deriving from both B and C, the compiler only
needs to make one copy of foo, and update the other parts to point to
it:

D's layout II:
// copied from A
int foo
// copied from B
<pointer to A>
int bar
// copied from C
<pointer to A>
int baz

But there may be a performance impact, because now whenever any
methods of B or C access foo, they have to go through that pointer.
Since A is a virtual base class, the methods can't make any
assumptions about where its members will actually be located in
memory.

Also, notice that the decision of whether A should be virtualized is
made in B and C -- not in D, where the diamond problem actually
occurs. That's a pain.

Finally, it should be clear now why the diamond problem doesn't exist
with interfaces. Interfaces only hold method pointers, not data
members whose values might change, and so it doesn't matter if they're
duplicated: you can't get into the situation where some methods see
one value there and other methods see a different value, because the
values are set once and they never change.

Jesse
 
I would love to have the diamond problem!

In other words. I would love to have Multiple Inheritance (MI) in c#.

I grew up with it and miss it dearly - and I never actually faced the
diamond problem.

I think MI is like sight or hearing - if you grew up with it and lose it,
its devastating, but if you never had it you dont know what you're missing.

When I model the world, I look for reusable orthoganal patterns, but I can
no longer implement naturally in c#. I have to adjust my view of the world
to cope with an SI language.
 
Listen to Mr. Diamond!

Norman Diamond said:
The Diamond solution was going to be explicit declarations where the
derived class would say which base class members would be unified and
which would be separated as separate members. Also it would have allowed
optional renaming in inheritance, so that maintenance programmers could
decide for themselves how to choose the lesser of two maintenance
nightmares every time it came up. But that was 19 years ago, it wasn't
for C++ or C#, and my then-employer had lied when they said they wanted me
to work on this kind of thing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top