Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > virtual inheritance / dreaded diamond problem

Reply
Thread Tools

virtual inheritance / dreaded diamond problem

 
 
Alexander Stippler
Guest
Posts: n/a
 
      07-14-2003
I've got an inheritance structure with two coupled "dreaded diamonds" like
shown below:
A
/ \
/ \
B C
\ / \
\ / \
D E
\ /
\ /
F

Now I get into some trouble with constructor calls. Default constructors get
called in situations, I would expect other constructors to be called. The
following example shows the problem:

#include <iostream>

class A
{
public:
A() { std::cerr << "A()" << std::endl; }
};

class B
: public virtual A
{
public:
B() { std::cerr << "B()" << std::endl; }
B(const A &a) { std::cerr << "B(const A& a)" << std::endl; }
};

class C
: public virtual A
{
public:
C() { std::cerr << "C()" << std::endl; }
};

class D
: public B,
public C
{
public:
D() { std::cerr << "D()" << std::endl; }
D(const B &b) : B(b) { std::cerr << "D(const B &b)" << std::endl; }
};

class E
: virtual public C
{
public:
E() { std::cerr << "E()" << std::endl; }
};

class F
: public D,
public E
{
public:
F() { std::cerr << "F()" << std::endl; }
F(const D &d) : D(d) { std::cerr << "F(const D &d)" << std::endl; }
};

int
main()
{
D d;
F f(d);
}

Of course all classes have also data members. What I do not understand is
the sequence of called constructors. Why does the initializer list of the
second constructor of F not call D:(const B &b). Is it about virtual
inheritance? Where am I wrong. How do I organize a structure like the above
correctly?

regards,
Alex
 
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
Is there any more benifits by virtual inheritance than resolving the"diamond problem" ? thomas C++ 5 11-27-2009 12:27 PM
'super' to only be used for diamond inheritance problems? Alex Hunsley Python 4 11-02-2005 01:53 PM
MRO problems with diamond inheritance? John Perks and Sarah Mount Python 13 05-03-2005 05:21 AM
diamond inheritance hides non-default constructor Tom C++ 3 10-26-2004 02:39 PM
virtual inheritance / dreaded diamond again Alexander Stippler C++ 1 08-26-2003 02:53 PM



Advertisments