By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,407 Members | 1,013 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,407 IT Pros & Developers. It's quick & easy.

Diamond problem

P: n/a
How does C++ and C# solve the Diamond problem? With the help of
interfaces that is.

Can anyone elaborate .......

Regards
Jan 17 '08 #1
Share this Question
Share on Google+
9 Replies


P: n/a
"weird0" <am********@gmail.comwrote in message
news:8e**********************************@l32g2000 hse.googlegroups.com...
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.
--
Anthony Jones - MVP ASP/ASP.NET
Jan 17 '08 #2

P: n/a
"weird0" <am********@gmail.comwrote in message
news:8e**********************************@l32g2000 hse.googlegroups.com...
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?

Jan 17 '08 #3

P: n/a
On 17 Jan, 13:04, "Todd Carnes" <toddcar...@gmail.comwrote:
"weird0" <amiredi...@gmail.comwrote in message

news:8e**********************************@l32g2000 hse.googlegroups.com...
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).

Jan 17 '08 #4

P: n/a
On 17 Jan, 13:09, DeveloperX <ianathomeag...@googlemail.comwrote:
On 17 Jan, 13:04, "Todd Carnes" <toddcar...@gmail.comwrote:
"weird0" <amiredi...@gmail.comwrote in message
news:8e**********************************@l32g2000 hse.googlegroups.com...
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*
Jan 17 '08 #5

P: n/a

"DeveloperX" <ia************@googlemail.comwrote in message
news:90**********************************@v29g2000 hsf.googlegroups.com...
On 17 Jan, 13:09, DeveloperX <ianathomeag...@googlemail.comwrote:
>On 17 Jan, 13:04, "Todd Carnes" <toddcar...@gmail.comwrote:
"weird0" <amiredi...@gmail.comwrote in message
>news:8e**********************************@l32g200 0hse.googlegroups.com...
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*

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.





Jan 17 '08 #6

P: n/a
On 17 Jan, 13:55, "Bill McCarthy" <B...@NOSPAM.comwrote:
"DeveloperX" <ianathomeag...@googlemail.comwrote in message

news:90**********************************@v29g2000 hsf.googlegroups.com...


On 17 Jan, 13:09, DeveloperX <ianathomeag...@googlemail.comwrote:
On 17 Jan, 13:04, "Todd Carnes" <toddcar...@gmail.comwrote:
"weird0" <amiredi...@gmail.comwrote in message
news:8e**********************************@l32g2000 hse.googlegroups.com....
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*

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" :)
Jan 17 '08 #7

P: n/a
"DeveloperX" <ia************@googlemail.comwrote in message
news:90**********************************@v29g2000 hsf.googlegroups.com...
On 17 Jan, 13:09, DeveloperX <ianathomeag...@googlemail.comwrote:
>On 17 Jan, 13:04, "Todd Carnes" <toddcar...@gmail.comwrote:
"weird0" <amiredi...@gmail.comwrote in message
>news:8e**********************************@l32g200 0hse.googlegroups.com...
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*

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

Todd

Jan 17 '08 #8

P: n/a
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.
"weird0" <am********@gmail.comwrote in message
news:8e**********************************@l32g2000 hse.googlegroups.com...
How does C++ and C# solve the Diamond problem? With the help of interfaces
that is.

Can anyone elaborate .......

Regards
Jan 18 '08 #9

P: n/a
On Jan 17, 4:11 am, weird0 <amiredi...@gmail.comwrote:
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
Jan 18 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.