473,408 Members | 2,032 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

virtual inheritance / dreaded diamond problem

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::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
Jul 19 '05 #1
0 4207

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Alexander Stippler | last post by:
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 /...
9
by: Michael Winter | last post by:
Until about 5 minutes ago, I was happy with my knowledge of virtual functions - then I read "Mixing interface and functional inheritance" posted by Kevin L. earlier today. All of a sudden, I found...
2
by: Corno | last post by:
Hi all, I know there are quite a few arguments in favor or against MI. I know there are a lot of usenet discussions about wether MI is actually needed or not. These discussions mention the...
3
by: puzzlecracker | last post by:
I am sure people are familiar with dirty diamond problem solved by virtual inheritance. look at the code below and tell me how many instances of the base class (in this case A) does class D...
4
by: Xavier | last post by:
Hi, I have a question, in a "dreaded diamond" situation, regarding the following code: ---- Begin code #include <iostream> using namespace std;
2
by: emma middlebrook | last post by:
Hi I come from a C++ background and so am familiar with the 'dreaded inheritance diamond' i.e. the ambiguity of which data to use when a class appears twice in the hierarchy. Here's a link for...
14
by: Bruno van Dooren | last post by:
Hi all, i am having a problems with inheritance. consider the following: class A { public: A(int i){;} };
3
by: Klaas Vantournhout | last post by:
Hi, Recently I obtained a problem with virtual inheritance when implementing it in multiple files. To present the problem I have included at the bottom of this post the code of the 4 files. I...
7
by: 2005 | last post by:
Hi What is the purpose of declaring a class with a "Virtual" in front? What is the benefit? Thanks
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.