virtual inheritance / dreaded diamond again

A

Alexander Stippler

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
 
R

Ron Natalie

Alexander Stippler said:
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
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top