Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > virtual inheritance

Reply
Thread Tools

virtual inheritance

 
 
Rahul
Guest
Posts: n/a
 
      04-13-2008
Hi Everyone,

I have the following class hierarchy,

A
B C
D

B and C are derived from A and D is derived from B and C. Now in
order to avoid the duplication problem, virtual inheritance can be
used. But i'm not sure as to where it should be used?

Should B and C inherit A virtually or should D inherit virtually B
and C?
I'm confused as i'm dealing with a case where A, B and C are provided
by different vendors. B knows that it has to inherit from A but is not
aware about anything else, so is the case with C.

Now how does the vendor providing class D solve the duplication
problem?

Thanks in advance ! ! !
 
Reply With Quote
 
 
 
 
peter koch
Guest
Posts: n/a
 
      04-13-2008
On 13 Apr., 15:11, Rahul <(E-Mail Removed)> wrote:
> Hi Everyone,
>
> *I have the following class hierarchy,
>
> * * * * *A
> * * B * * * *C
> * * * * *D
>
> *B and C are derived from A and D is derived from B and C. Now in
> order to avoid the duplication problem, virtual inheritance can be
> used. But i'm not sure as to where it should be used?
>
> *Should B and C inherit A virtually or should D inherit virtually B
> and C?


B and C should inherit virtually from A.

> I'm confused as i'm dealing with a case where A, B and C are provided
> by different vendors. B knows that it has to inherit from A but is not
> aware about anything else, so is the case with C.
>
> *Now how does the vendor providing class D solve the duplication
> problem?


By choosing an A. Whenever there is an access to an A object, you must
specify if it is the B or the C class' A object you want to use (or
both!). Admittedly this is a "hackish" solution, but I know no better
way to solve it.

/Peter
 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      04-13-2008
Rahul wrote:
> Should B and C inherit A virtually or should D inherit virtually B
> and C?
> I'm confused as i'm dealing with a case where A, B and C are provided
> by different vendors. B knows that it has to inherit from A but is not
> aware about anything else, so is the case with C.


B and C must inherit virtually from A. If they don't and you can't
modify them, then unfortunately there's no way to do it. (This is
because existing compiled code for B and C assume non-virtual
inheritance. This compiled code would be different if the inheritance
was virtual. Thus changing it to virtual would require recompiling that
code.)
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-13-2008
On 13 avr, 15:11, Rahul <(E-Mail Removed)> wrote:

> I have the following class hierarchy,


> A
> B C
> D


> B and C are derived from A and D is derived from B and C. Now in
> order to avoid the duplication problem, virtual inheritance can be
> used. But i'm not sure as to where it should be used?


> Should B and C inherit A virtually or should D inherit virtually B
> and C?


B and C should inherit virtually from A.

> I'm confused as i'm dealing with a case where A, B and C are provided
> by different vendors. B knows that it has to inherit from A but is not
> aware about anything else, so is the case with C.


So they should inherit virtually, unless they need a private
copy of A, which isn't shared. In general, unless you know the
full hierarchy, virtual inheritance should be the default.

> Now how does the vendor providing class D solve the
> duplication problem?


By getting the vendors of B and C to do things correctly.

Seriously, it can be a problem, because this issue seems to be
frequently ignored. (Logically, one wishes that virtual
inheritance had been the default.)

The first thing you might ask is: does it matter? If A has no
data members (which is most often the case when classes are
inheriting from it), it might not matter. You end up with two A
objects (and two A* for each D object), but it typically doesn't
matter which one gets used.

--
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
Virtual inheritace -- when one inheritance of the base is virtual andthe other isn't. pauldepstein@att.net C++ 1 03-14-2009 03:45 PM
virtual inheritance and virtual function. Ashwin C++ 2 08-01-2006 12:48 PM
mul. inheritance & overloading operator new/delete solved by virtual base inheritance? cppsks C++ 0 10-27-2004 07:49 PM
Should 'public virtual' always become 'private virtual'? & using private inheritance qazmlp C++ 19 02-04-2004 12:37 AM
Private access modifier and Inheritance (Inheritance implementation in Java) maxw_cc Java 1 12-21-2003 11:38 AM



Advertisments