Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > oop inheritance graph

Reply
Thread Tools

oop inheritance graph

 
 
a
Guest
Posts: n/a
 
      06-25-2007
Hi,
I have an oop inheritance graph problem.
What is the difference betweent the following 2 inheritance graph?
How does the C++ solve the naming conflict problem for multiple inheritance
problem?

Thanks

A
/ \
B C
\ /
D


A A
| |
B C
\ /
D


 
Reply With Quote
 
 
 
 
aaragon
Guest
Posts: n/a
 
      06-25-2007
On Jun 24, 11:25 pm, "a" <(E-Mail Removed)> wrote:
> Hi,
> I have an oop inheritance graph problem.
> What is the difference betweent the following 2 inheritance graph?
> How does the C++ solve the naming conflict problem for multiple inheritance
> problem?
>
> Thanks
>
> A
> / \
> B C
> \ /
> D
>
> A A
> | |
> B C
> \ /
> D


There is no difference between the two graphs that you draw since both
B and C inherit from A in both graphs. You have to use "virtual
inheritance" for class A. Scott Meyers discusses this point in detail
in his book Effective C++ (item 43). You may want to avoid this kind
of inheritance graph. The naming convention is defined by the closest
definition of a function to D. For example, if A defines foo() and
this is redefined in C, D uses the C version of foo(). Hope it helps.

 
Reply With Quote
 
 
 
 
Sumit Rajan
Guest
Posts: n/a
 
      06-25-2007
a wrote:
> Hi,
> I have an oop inheritance graph problem.
> What is the difference betweent the following 2 inheritance graph?
> How does the C++ solve the naming conflict problem for multiple inheritance
> problem?
>
> Thanks
>
> A
> / \
> B C
> \ /
> D
>
>
> A A
> | |
> B C
> \ /
> D
>
>


Please see:
http://www.parashift.com/c++-faq-lit...heritance.html

--
Sumit Rajan <(E-Mail Removed)>
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      06-25-2007
On Jun 25, 6:25 am, "a" <(E-Mail Removed)> wrote:
> I have an oop inheritance graph problem.
> What is the difference betweent the following 2 inheritance graph?
> How does the C++ solve the naming conflict problem for multiple inheritance
> problem?


> A
> / \
> B C
> \ /
> D
>
> A A
> | |
> B C
> \ /
> D


In the first, you only have a single instance of A; both B and C
have the same instance as their base class. In the second, you
have two instances. In C++, you use virtual inheritance to
achieve the first, e.g.:

First:

class A {} ;
class B : public virtual A {} ;
class C : public virtual A {} ;
class D : public B, public C {} ;

Second:

class A {} ;
class B : public A {} ;
class C : public A {} ;
class D : public B, public C {} ;

Note the presence of the keyword "virtual" in the inheritance in
the first.

I'm not too sure what you mean by "naming conflict". In the
first, there is only one instance of each class, so there is no
ambituity when referring to a base class. In the second, in D
(or when using an object of type D), any direct reference to A
is ambiguous, since the compiler doesn't know which one; to
disabmiguate, first refer to B or C, e.g. B::A::... or C::A::...

--
James Kanze (GABI Software, from CAI) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      06-25-2007
aaragon wrote:
> On Jun 24, 11:25 pm, "a" <(E-Mail Removed)> wrote:
>> Hi,
>> I have an oop inheritance graph problem.
>> What is the difference betweent the following 2 inheritance graph?
>> How does the C++ solve the naming conflict problem for multiple inheritance
>> problem?
>>
>> Thanks
>>
>> A
>> / \
>> B C
>> \ /
>> D
>>
>> A A
>> | |
>> B C
>> \ /
>> D

>
> There is no difference between the two graphs that you draw since both
> B and C inherit from A in both graphs. You have to use "virtual
> inheritance" for class A. Scott Meyers discusses this point in detail
> in his book Effective C++ (item 43). You may want to avoid this kind
> of inheritance graph. The naming convention is defined by the closest
> definition of a function to D. For example, if A defines foo() and
> this is redefined in C, D uses the C version of foo(). Hope it helps.
>


No, you don't "have to" use virtual inheritance. That's the difference
between them: the top one uses it and the bottom one doesn't.

--

-- 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
 
a
Guest
Posts: n/a
 
      06-26-2007

"James Kanze" <(E-Mail Removed)>
???????:(E-Mail Removed) groups.com...
On Jun 25, 6:25 am, "a" <(E-Mail Removed)> wrote:
> I have an oop inheritance graph problem.
> What is the difference betweent the following 2 inheritance graph?
> How does the C++ solve the naming conflict problem for multiple

inheritance
> problem?


> A
> / \
> B C
> \ /
> D
>
> A A
> | |
> B C
> \ /
> D


In the first, you only have a single instance of A; both B and C
have the same instance as their base class. In the second, you
have two instances. In C++, you use virtual inheritance to
achieve the first, e.g.:

First:

class A {} ;
class B : public virtual A {} ;
class C : public virtual A {} ;
class D : public B, public C {} ;

Second:

class A {} ;
class B : public A {} ;
class C : public A {} ;
class D : public B, public C {} ;

Note the presence of the keyword "virtual" in the inheritance in
the first.

I'm not too sure what you mean by "naming conflict". In the
first, there is only one instance of each class, so there is no
ambituity when referring to a base class. In the second, in D
(or when using an object of type D), any direct reference to A
is ambiguous, since the compiler doesn't know which one; to
disabmiguate, first refer to B or C, e.g. B::A::... or C::A::...

--
James Kanze (GABI Software, from CAI) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Thank you for your reply.
The naming conflict may occur between B and C, since they both inherited
from A. Both B and C have the properties inherited from A, and they have the
same name. So, naming conflict may occurs. I understand the purpose of the
keyword "virtual". But, in terms of "instance", what is the difference
between class B : public virtual A {} and class B : public A {}?

Thanks



 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      06-26-2007
On Jun 26, 7:51 am, "a" <(E-Mail Removed)> wrote:
> "James Kanze" <(E-Mail Removed)>
> > ???????:(E-Mail Removed) groups.com...
> > On Jun 25, 6:25 am, "a" <(E-Mail Removed)> wrote:


> > > I have an oop inheritance graph problem. What is the
> > > difference betweent the following 2 inheritance graph? How
> > > does the C++ solve the naming conflict problem for multiple
> > > inheritance problem?


> > > A
> > > / \
> > > B C
> > > \ /
> > > D


> > > A A
> > > | |
> > > B C
> > > \ /
> > > D


> > In the first, you only have a single instance of A; both B and C
> > have the same instance as their base class. In the second, you
> > have two instances. In C++, you use virtual inheritance to
> > achieve the first, e.g.:


> > First:


> > class A {} ;
> > class B : public virtual A {} ;
> > class C : public virtual A {} ;
> > class D : public B, public C {} ;


> > Second:


> > class A {} ;
> > class B : public A {} ;
> > class C : public A {} ;
> > class D : public B, public C {} ;


> > Note the presence of the keyword "virtual" in the inheritance in
> > the first.


> > I'm not too sure what you mean by "naming conflict". In the
> > first, there is only one instance of each class, so there is no
> > ambituity when referring to a base class. In the second, in D
> > (or when using an object of type D), any direct reference to A
> > is ambiguous, since the compiler doesn't know which one; to
> > disabmiguate, first refer to B or C, e.g. B::A::... or C::A::...


> The naming conflict may occur between B and C, since they both
> inherited from A. Both B and C have the properties inherited
> from A, and they have the same name. So, naming conflict may
> occurs.


By "properties", I presume you mean member variables, or ? And
by conflict, you mean ambiguity. If you use virtual inheritance
(the first schema), then there is no ambiguity, since there is
only one instance of A. If you don't use virtual inheritance,
then you refer to either B::A::memberOfA or C::A::memberOfA, in
order to distinguish.

> I understand the purpose of the
> keyword "virtual". But, in terms of "instance", what is the difference
> between class B : public virtual A {} and class B : public A {}?


For class B alone, none, at least from the user's point of view.
It's only when you start to derive further that virtual
inheritance has an effect. With the virtual inheritance as
above, the class D has exactly one instance of A, and something
like B::A::someMember and C::A::someMember refer to exactly the
same object. Without the virtual inheritance, D will have two
instances of A, one as a sub-object of B, and the other as a
sub-object of C, and B::A::someMember and C::A::someMember will
refer to different objects.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 
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
Using Boost Graph Library to create very large graph Almoni C++ 0 01-17-2010 05:13 AM
[Boost.Graph] graph.vertices property creates new objects George Sakkis Python 1 01-29-2007 11:09 PM
Help with initialization of graph (Boost Graph Library) Jef Driesen C++ 3 01-24-2006 01:44 PM
Missing Graph.h and (Graph.lib) woes - any help Dr Ann Huxtable C Programming 6 12-21-2004 11:15 AM
GD::Graph: "mixed" graph doesn't recognize "area" graph type Emilio Mayorga Perl Misc 6 10-08-2003 02:14 AM



Advertisments