473,385 Members | 1,676 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,385 software developers and data experts.

diamond inheritance hides non-default constructor

Tom
Hello, I've searched groups and the std unsuccessfully for an explanation
of the following - I'd appreciate any comments you may have.

Given the following diamond multiple inheritance pattern, with the base
class having 2 constructors, only the default constructor is called. Is
there a way to require the other constructor to be used?

First, the output:

Base default constructor
Derived1:
Derived2:
Join:
Main:

class Base
{
public:
Base ()
{ cout << "Base default constructor" << endl; }
Base (string const & useMsg)
: msg (useMsg)
{ cout << "Base: " << msg << endl; }
virtual ~Base() {}
string msg;
};

class Derived1 : virtual public Base
{
public:
Derived1 (string const & useMsg)
: Base (useMsg)
{ cout << "Derived1: " << msg << endl; }
};

class Derived2 : virtual public Base
{
public:
Derived2 ()
: Base ()
{ cout << "Derived2: " << msg << endl; }
};

class Join : public Derived1, public Derived2
{
public:
Join ()
: Derived1 ( string ("Hello") ), Derived2 ()
{ cout << "Join: " << msg << endl; }
};

int main ()
{
Join join;
cout << "Main: " << join.msg << endl;
return 0;
}

Jul 22 '05 #1
3 1706
class Join : public Derived1, public Derived2
{
public:
Join ()
: Derived1 ( string ("Hello") ), Derived2 ()
{ cout << "Join: " << msg << endl; }
};


Join() : Derived1( "Hello" ), Derived2(), Base("this is what I was
missing!")
{

}
-JKop
Jul 22 '05 #2
Tom wrote:
Hello, I've searched groups and the std unsuccessfully for an explanation
of the following - I'd appreciate any comments you may have.

Given the following diamond multiple inheritance pattern, with the base
class having 2 constructors, only the default constructor is called. Is
there a way to require the other constructor to be used?

First, the output:

Base default constructor
Derived1:
Derived2:
Join:
Main:

class Base
{
public:
Base ()
{ cout << "Base default constructor" << endl; }
Base (string const & useMsg)
: msg (useMsg)
{ cout << "Base: " << msg << endl; }
virtual ~Base() {}
string msg;
};

class Derived1 : virtual public Base
{
public:
Derived1 (string const & useMsg)
: Base (useMsg)
{ cout << "Derived1: " << msg << endl; }
};

class Derived2 : virtual public Base
{
public:
Derived2 ()
: Base ()
{ cout << "Derived2: " << msg << endl; }
};

class Join : public Derived1, public Derived2
{
public:
Join ()
: Derived1 ( string ("Hello") ), Derived2 ()
{ cout << "Join: " << msg << endl; }
};

int main ()
{
Join join;
cout << "Main: " << join.msg << endl;
return 0;
}

Virtual base class initialisation is done at the level of the most derived
class. In your case the 'Join' c-tor should have

Base(???)

in its initialiser list (along with other things). Replace the ??? with
the appropriate expression you want to be evaluated and passed to the
Base's c-tor. I suspect it will be

Base("Hello")

Victor
Jul 22 '05 #3
Tom
Thanks for your answers.
I finally found your quote in the std doc under initialization (section 12),
not in the section on inheritance (section 10). The explanation is in the
sentence following your quote of it:

All sub-objects representing virtual base classes are initialized by
the constructor of the most derived class. If the constructor of
the most derived class does not specify a mem-initializer for a
virtual base class V, then V's default constructor is called to
initialize the virtual base class subobject.

Tom
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:Zl***************@newsread1.dllstx09.us.to.ve rio.net...

Virtual base class initialisation is done at the level of the most derived
class. In your case the 'Join' c-tor should have

Base(???)

in its initialiser list (along with other things). Replace the ??? with
the appropriate expression you want to be evaluated and passed to the
Base's c-tor. I suspect it will be

Base("Hello")

Victor

Jul 22 '05 #4

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

Similar topics

13
by: John Perks and Sarah Mount | last post by:
Trying to create the "lopsided diamond" inheritance below: >>> class B(object):pass >>> class D1(B):pass >>> class D2(D1):pass >>> class D(D1, D2):pass Traceback (most recent call last): File...
4
by: Steven T. Hatton | last post by:
Below is some code I wrote to get a better understanding of the dynamic verses static type resolution. My intention was to see if there was a way to use references for the static access to the...
5
by: Tony Johansson | last post by:
Hello Experts! It it correct to say that a solution to the diamond problem is to use virtual inheritance with virtual base classes. //Tony
3
by: Tony Johansson | last post by:
Hello Experts!! Assume you have the following diamond inheritance structure. Assume the following at the top you have the Ancestor class and below this class at the same level we have class...
11
by: coinjo | last post by:
I need to write a program which takes a number as an input and prints a diamond of # and $. The number of rows in the shape is equal to the number entered by the user. My program should display the...
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...
3
by: enchantingdb | last post by:
I have an exam tomorrow that covers the perceived advantages and disadvantages of object oriented programming, in particular polymorphism, inheritance and encapsulation. I know the advantages but...
14
by: jasson118 | last post by:
i am a newber to C++ and have trouble with one of the problem from the book. can anyone able to use nested loops to display a diamond shape with " * ". * *** ***** ******* ********* *******
9
by: weird0 | last post by:
How does C++ and C# solve the Diamond problem? With the help of interfaces that is. Can anyone elaborate ....... Regards
6
by: Rocketmagnet | last post by:
Hi all, I have been kind of forced (honestly) into writing a class structure which contains a Diamond of Death, and I'm not entirely sure what to do about it. This is a simplified version of my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.