473,322 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Diamond problem

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

Can anyone elaborate .......

Regards
Jan 17 '08 #1
9 10929
"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
"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
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
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

"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
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: John Perks and Sarah Mount | last post by:
Trying to create the "lopsided diamond" inheritance below: >>> class B(object):pass >>> class D1(B):pass >>> class D2(D1):pass >>> class D(D1, D2):pass Traceback (most recent call last): File...
5
by: Tony Johansson | last post by:
Hello Experts! It it correct to say that a solution to the diamond problem is to use virtual inheritance with virtual base classes. //Tony
11
by: coinjo | last post by:
I need to write a program which takes a number as an input and prints a diamond of # and $. The number of rows in the shape is equal to the number entered by the user. My program should display the...
2
by: emma middlebrook | last post by:
Hi I come from a C++ background and so am familiar with the 'dreaded inheritance diamond' i.e. the ambiguity of which data to use when a class appears twice in the hierarchy. Here's a link for...
3
by: mitchellpal | last post by:
guys.... help me out here... my code is running halfway... how do i complete the other right half....... pp.. the user should input an odd number btw 0 and 20 then the program displays th shape as...
1
sanjay123456
by: sanjay123456 | last post by:
dear friends how can we solve diamond problem by scope reslution operator ? sanjay
14
by: jasson118 | last post by:
i am a newber to C++ and have trouble with one of the problem from the book. can anyone able to use nested loops to display a diamond shape with " * ". * *** ***** ******* ********* *******
4
by: Pallav singh | last post by:
I am Facing Problem while creating object of Diamond Ring problem solving using Template Kindly let me known where i am committing ERROR Thanks Pallav #include<iostream.h> #include<string.h>
6
by: Rocketmagnet | last post by:
Hi all, I have been kind of forced (honestly) into writing a class structure which contains a Diamond of Death, and I'm not entirely sure what to do about it. This is a simplified version of my...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.