Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > ambiguity in diamond inheritance

Reply
Thread Tools

ambiguity in diamond inheritance

 
 
karthikbalaguru
Guest
Posts: n/a
 
      03-06-2008
Hi,

I wonder why is there an ambiguity in diamond
inheritance when we try to call the method in the
parent class from the class derived from multiple
(two)childs of the parent class.
Why C++ does not handle it internally and give us the
path to access the method in the parent class ?
Any constraints ?

Thx in advans,
Karthik Balaguru
 
Reply With Quote
 
 
 
 
keith@bytebrothers.co.uk
Guest
Posts: n/a
 
      03-06-2008
On 6 Mar, 11:38, karthikbalaguru <karthikbalagur...@gmail.com> wrote:
>
> I wonder why is there an ambiguity in diamond
> inheritance when we try to call the method in the
> parent class from the class derived from multiple
> (two)childs of the parent class.
> Why C++ does not handle it internally and give us the
> path to access the method in the parent class ?
> Any constraints ?


See
http://www.parashift.com/c++-faq-lit....html#faq-25.9
 
Reply With Quote
 
 
 
 
dizzy
Guest
Posts: n/a
 
      03-06-2008
karthikbalaguru wrote:

> Hi,
>
> I wonder why is there an ambiguity in diamond
> inheritance when we try to call the method in the
> parent class from the class derived from multiple
> (two)childs of the parent class.
> Why C++ does not handle it internally and give us the
> path to access the method in the parent class ?
> Any constraints ?


1. there are no C++ methods, there are only member functions of various
types, let's supose you mean normal (non static, non virtual) member
function

2. if it were to be able to call it, on what instance of the common base it
should do it? Because if you have a diamond, you got 2 instances of the
common base, one though one branch of the diamond and the other through the
other so it should call the member function on which of these instances?
Thus the ambiguity

If instead, you wanted to have a single common base even if it's inherited
(indirectly) multiple times then you should look over "virtual inheritance"
in your C++ book.

--
Dizzy

 
Reply With Quote
 
happyasaclam111@gmail.com
Guest
Posts: n/a
 
      03-07-2008
On Mar 6, 3:38*am, karthikbalaguru <karthikbalagur...@gmail.com>
wrote:
> Hi,
>
> I wonder why is there an ambiguity in diamond
> inheritance when we try to call the method in the
> parent class from the class derived from multiple
> (two)childs of the parent class.
> Why C++ does not handle it internally and give us the
> path to access the method in the parent class ?
> Any constraints ?
>
> Thx in advans,
> Karthik Balaguru


Let's say we have four classes: A, B, C, and D
B & C both derive from A
D derives from B & C

D essentially "contains" two A sub-objects. One for the B part and
one for the C part. Consider the following code:

#include <iostream>

class A
{
int n;
public:
A(int i) : n(i) {};
virtual void f() { std::cout << "From A: " << n << std::endl; }
};

class B : public A
{
public:
B() : A(30) {};
};

class C : public A
{
public:
C() : A(50) {};
};

class D : public B, public C {};

int main()
{
D d;
d.C::f(); //prints 50
d.B::f(); //prints 30
return 0;
}

If both B & C had derived virtually from A:
class B : public virtual A {/*...*/};
class C : public virtual A {/*...*/};
Then D would only have one A sub-object. In this case, D would be
responsible for constructing A.
 
Reply With Quote
 
karthikbalaguru
Guest
Posts: n/a
 
      03-07-2008
On Mar 6, 6:19*pm, dizzy <di...@roedu.net> wrote:
> karthikbalaguru wrote:
> > Hi,

>
> > I wonder why is there an ambiguity in diamond
> > inheritance when we try to call the method in the
> > parent class from the class derived from multiple
> > (two)childs of the parent class.
> > Why C++ does not handle it internally and give us the
> > path to access the method in the parent class ?
> > Any constraints ?

>
> 1. there are no C++ methods, there are only member functions of various
> types, let's supose you mean normal (non static, non virtual) member
> function
>
> 2. if it were to be able to call it, on what instance of the common base it
> should do it? Because if you have a diamond, you got 2 instances of the
> common base, one though one branch of the diamond and the other through the
> other so it should call the member function on which of these instances?
> Thus the ambiguity


I understand the Diamond Problem and the solution of "Virtual
Inheritance"
that is used to overcome ambiguity.
But, i wonder why should the Diamond problem arise. This should
be taken care by C++ internally and resolved. Why C++ could not
handle it internally without asking us to do the 'virtual
inheritance'?

Thx in advans,
Karthik Balaguru
 
Reply With Quote
 
Shobhit Gupta
Guest
Posts: n/a
 
      03-07-2008
> I understand the Diamond Problem and the solution of "Virtual
> Inheritance"
> that is used to overcome ambiguity.
> But, i wonder why should the Diamond problem arise. This should
> be taken care by C++ internally and resolved. Why C++ could not
> handle it internally without asking us to do the 'virtual
> inheritance'?



If c++ would automatically resolve the ambiguity, then how would you
be able to have 2 separate instances ?

Base Base
| |
| |
| |
Der1 Der2
\ /
\ /
\ /
Join


I know it would be a bad design to have such a structure, but just in
case someone wanted to have a non-diamond structure.... c++ allows
it.

 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      03-07-2008
On 2008-03-06 23:34:45 -0500, karthikbalaguru
<> said:

>
> I understand the Diamond Problem and the solution of "Virtual
> Inheritance"
> that is used to overcome ambiguity.
> But, i wonder why should the Diamond problem arise. This should
> be taken care by C++ internally and resolved. Why C++ could not
> handle it internally without asking us to do the 'virtual
> inheritance'?
>


"The Diamond Problem" arises because the writer of a class hierarchy
didn't understand how inheritance works.

struct A
{
int i;
};

struct B : A
{
};

struct C : A
{
};

struct D : B, C
{
void f();
};

void D::f()
{
i = 3; // error: ambiguous
}

There are two separate "i" members in D. There's no way the compiler
can "handle it internally". You have to say what you mean:

void D::f()
{
B::i = 3; // OK: B's "i" member
}

If you don't want to have two "I" members in D, then the inheritance
hierarchy as written above is wrong. If the design calls for only one A
subobject, you do that by making A a virtual base everywhere. Then
there's only one "i" member in D. But note that that's a design
decision, not an implementation decision. Either you have only one A
subobject, in which case "i" is unambibuous, or you have two, and you
have to say which one you mean.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      03-07-2008
On 2008-03-07 08:07:37 -0500, Shobhit Gupta <> said:

>
> If c++ would automatically resolve the ambiguity, then how would you
> be able to have 2 separate instances ?
>
> Base Base
> | |
> | |
> | |
> Der1 Der2
> \ /
> \ /
> \ /
> Join
>
>
> I know it would be a bad design to have such a structure, but just in
> case someone wanted to have a non-diamond structure.... c++ allows
> it.


It's not a bad design if it's the correct solution to the problem. For
example, if Base is a node that can for a linked list, Join can be an
element in two lists.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

 
Reply With Quote
 
Shobhit Gupta
Guest
Posts: n/a
 
      03-07-2008
> It's not a bad design if it's the correct solution to the problem. For
> example, if Base is a node that can for a linked list, Join can be an
> element in two lists.



That sounds right. I was thinking of other scenarios, like having BASE
class contain some information. (In that case its better to use
composition.)
But thats something very true, if anything is a correct solution, its
probably the right design for the user.
 
Reply With Quote
 
happyasaclam111@gmail.com
Guest
Posts: n/a
 
      03-07-2008
On Mar 6, 8:34*pm, karthikbalaguru <karthikbalagur...@gmail.com>
wrote:

> I understand the Diamond Problem and the solution of "Virtual
> Inheritance"
> that is used to overcome ambiguity.
> But, i wonder why should the Diamond problem arise. This should
> be taken care by C++ internally and resolved. Why C++ could not
> handle it internally without asking us to do the 'virtual
> inheritance'?
>
> Thx in advans,
> Karthik Balaguru


Read my example above and consider this:
d.C::f() prints out 50
d.B::f() prints out 30
What should d.f() print out?

Should the compiler just guess which one you meant? Or are you asking
why non-virtual inheritance is the default?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple Inheritance ambiguity but not really? Nick Overdijk C++ 6 04-12-2009 09:55 AM
Multiple inheritance/overloading ambiguity - is this behaviour intentional? Adam Nielsen C++ 3 09-27-2007 12:39 AM
Virtual Inheritance Ambiguity mijobee C++ 12 10-25-2006 09:02 PM
Multiple inheritance ambiguity Daniel Mitchell C++ 2 05-25-2006 07:46 PM
Multiple Inheritance Ambiguity =?ISO-8859-1?Q?IvD=B2?= C++ 5 01-14-2005 01:52 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57