virtual inheritance / dreaded diamond again

Discussion in 'C++' started by Alexander Stippler, Aug 26, 2003.

  1. Hello,

    I want to build up an inheritance structure looking like two coupled dreaded
    diamonds. It's similar to an old posting of me. Now my question is, where
    to use the keyword virtual.

    CV
    / \
    / \
    V CDV
    \ / \
    \ / \
    DV CMI
    \ /
    \ /
    MI

    I placed virtual like this:

    V : public virtual CV
    CDV : public virtual CV
    DV : public virtual CDV, public V
    CMI : public virtual CDV
    MI : public DV, public CMI

    My porblem is, that the construction process of an MI does not do the
    conctructor calls, I would expect. There is a constructor

    MI(const CDV &rhs) : DV(rhs) {} // CMI has a default constructor.

    which calls

    DV(const CDV &rhs) : V(rhs) {} // CDV has a default constructor.

    and now I would expect

    V(const CV &rhs) {} // CV has a default constructor.

    to be called, but actually

    V() {}

    is called. And here I wonder. Where am I wrong. I hope I provided enough
    information. Else ask please. All classes are template classes by the way,
    if relevant.

    regards,
    alex
    Alexander Stippler, Aug 26, 2003
    #1
    1. Advertising

  2. Alexander Stippler

    Ron Natalie Guest

    "Alexander Stippler" <-ulm.de> wrote in message news:-ulm.de...

    >
    > V() {}
    >
    > is called. And here I wonder. Where am I wrong. I hope I provided enough
    > information. Else ask please. All classes are template classes by the way,
    > if relevant.


    All virtual base classes are initialized by the most derived object before any
    non-virtual bases. It is the constructor of your most derived class (MI) that
    needs to pass any arguments to the virtual bases.

    MI(const CDV& rhs) : DV(rhs), CV(rhs) { }

    or the like.

    The order of construction of your heirarchy is:
    CV, CDV, V, DV, CMI, MI
    Ron Natalie, Aug 26, 2003
    #2
    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. Alexander Stippler

    virtual inheritance / dreaded diamond problem

    Alexander Stippler, Jul 14, 2003, in forum: C++
    Replies:
    0
    Views:
    1,870
    Alexander Stippler
    Jul 14, 2003
  2. Tom
    Replies:
    3
    Views:
    495
  3. John Perks and Sarah Mount

    MRO problems with diamond inheritance?

    John Perks and Sarah Mount, May 1, 2005, in forum: Python
    Replies:
    13
    Views:
    590
    Michele Simionato
    May 3, 2005
  4. Alex Hunsley
    Replies:
    4
    Views:
    357
    Colin J. Williams
    Nov 2, 2005
  5. thomas
    Replies:
    5
    Views:
    593
    Gert-Jan de Vos
    Nov 27, 2009
Loading...

Share This Page