virtual inheritance

R

Rahul

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 ! ! !
 
P

peter koch

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
 
J

Juha Nieminen

Rahul said:
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.)
 
J

James Kanze

I have the following class hierarchy,
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,084
Latest member
HansGeorgi

Latest Threads

Top