virtual inheritance

Discussion in 'C++' started by Rahul, Apr 13, 2008.

  1. Rahul

    Rahul Guest

    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 ! ! !
     
    Rahul, Apr 13, 2008
    #1
    1. Advertising

  2. Rahul

    peter koch Guest

    On 13 Apr., 15:11, Rahul <> 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
     
    peter koch, Apr 13, 2008
    #2
    1. Advertising

  3. 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.)
     
    Juha Nieminen, Apr 13, 2008
    #3
  4. Rahul

    James Kanze Guest

    On 13 avr, 15:11, Rahul <> 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:
    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
     
    James Kanze, Apr 13, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. maxw_cc
    Replies:
    1
    Views:
    3,151
    Martijn van Steenbergen
    Dec 21, 2003
  2. qazmlp
    Replies:
    19
    Views:
    800
    Daniel T.
    Feb 4, 2004
  3. cppsks
    Replies:
    0
    Views:
    827
    cppsks
    Oct 27, 2004
  4. Ashwin
    Replies:
    2
    Views:
    351
    Pierre Barbier de Reuille
    Aug 1, 2006
  5. Replies:
    1
    Views:
    469
    Alf P. Steinbach
    Mar 14, 2009
Loading...

Share This Page