473,657 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4226

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

Similar topics

1
2013
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 / \ / \ V CDV
9
3001
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 out that you can inherit using the virtual keyword: class A { } class B : public *virtual* A
2
3591
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 'dangers' of MI. The problem is; I know that there are dangers concerned with MI and I know how I can avoid them, I just don't have a clear idea of what the dangers actually are and if they are applicable to to the situations I encounter. The only...
3
1435
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 receive? class A { : :
4
4611
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
2412
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 anyone not familiar: http://www.parashift.com/c++-faq-lite/multiple-inheritance.html#faq-25.8 .. Base / \
14
1915
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
2350
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 show results with the gnu compiler but i have the same results with the intel compiler. file1.cpp compiles without a problem, the problem occurs when compiling file2.cpp. It complains about not finding the constructor of the class base.
7
4874
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
8826
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5632
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4155
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4306
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.