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

Help: virtual inheritance, non-default constructor

When compiling the following program, I get an error on the line specified:
The base class "B" cannot be initialized because it does not have a default
constructor.

If I remove the "virtual" inheritance of B (so that it uses plain
inheritance), it compiles fine. It also compiles if I invoke the
constructor for B explicitly, as shown in the comment after the error. I'm
not aware of any reason that virtual inheritance should be special in this
respect. What's wrong?

class B
{
public:
B(int){}
};

class C : virtual public B
{
public:
C():B(1){}
};

class D : public C
{
public:
D(){} // error here
// D():B(1){} - this however compiles OK
};

int main()
{
return 0;
}
Jul 19 '05 #1
10 7205

"jeffc" <no****@nowhere.com> wrote in message news:3f********@news1.prserv.net...
When compiling the following program, I get an error on the line specified:
The base class "B" cannot be initialized because it does not have a default
constructor.

The initialization of a virtual base is done by the most derived object.
Your class D must provide the necessary initializers for the virtual base
B. The virtual base is initialized just once and before any of the non-virtual
bases are initialized.
Jul 19 '05 #2

"jeffc" <no****@nowhere.com> wrote in message
news:3f********@news1.prserv.net...
When compiling the following program, I get an error on the line specified: The base class "B" cannot be initialized because it does not have a default constructor.

If I remove the "virtual" inheritance of B (so that it uses plain
inheritance), it compiles fine. It also compiles if I invoke the
constructor for B explicitly, as shown in the comment after the error. I'm not aware of any reason that virtual inheritance should be special in this
respect. What's wrong?


I finally found a reference to this problem in "Effective C++". Meyers
states that "arguments are specified in the member initialization lists of
the classes *most derived* from the base. As a result, the class
initializing a virtual base may be arbitrarily far from it in the
inheritance graph, and furthermore, the class performing the initialization
can change as new classes are added to the hierarchy."

So, 3 questions:
1) why is this so?
2) is he implying that once a new class is added to the hierarchy, the
initialization must be performed there *instead of* in the base class?
(because this seems inconsistent with what happens in the code I posted -
both the derived class and base class initialize the virtual base.
3) why does this make any difference at all, since I'm not even using
multiple inheritance? Is it merely a compiler implementation issue?
Jul 19 '05 #3
jeffc wrote:
"jeffc" <no****@nowhere.com> wrote in message
news:3f********@news1.prserv.net...
When compiling the following program, I get an error on the line


specified:
The base class "B" cannot be initialized because it does not have a


default
constructor.

If I remove the "virtual" inheritance of B (so that it uses plain
inheritance), it compiles fine. It also compiles if I invoke the
constructor for B explicitly, as shown in the comment after the error.


I'm
not aware of any reason that virtual inheritance should be special in this
respect. What's wrong?

I finally found a reference to this problem in "Effective C++". Meyers
states that "arguments are specified in the member initialization lists of
the classes *most derived* from the base. As a result, the class
initializing a virtual base may be arbitrarily far from it in the
inheritance graph, and furthermore, the class performing the initialization
can change as new classes are added to the hierarchy."

So, 3 questions:
1) why is this so?
2) is he implying that once a new class is added to the hierarchy, the
initialization must be performed there *instead of* in the base class?
(because this seems inconsistent with what happens in the code I posted -
both the derived class and base class initialize the virtual base.
3) why does this make any difference at all, since I'm not even using
multiple inheritance? Is it merely a compiler implementation issue?


IIRC in my copy of Meyer's he starts of with "Just when you
thought you understood it, they change the rules!" or some
such. A page onwards I believe he gives an explaination of why.

Jul 19 '05 #4

"lilburne" <li******@godzilla.net> wrote in message
news:bo*************@ID-203936.news.uni-berlin.de...

So, 3 questions:
1) why is this so?
2) is he implying that once a new class is added to the hierarchy, the
initialization must be performed there *instead of* in the base class?
(because this seems inconsistent with what happens in the code I posted - both the derived class and base class initialize the virtual base.
3) why does this make any difference at all, since I'm not even using
multiple inheritance? Is it merely a compiler implementation issue?


IIRC in my copy of Meyer's he starts of with "Just when you
thought you understood it, they change the rules!" or some
such. A page onwards I believe he gives an explaination of why.


He does say that - in the next subsection (which deals with virtual
functions). But I didn't see further explanation.
Jul 19 '05 #5

"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f*********************@news.newshosting.com. ..

"jeffc" <no****@nowhere.com> wrote in message news:3f********@news1.prserv.net...
When compiling the following program, I get an error on the line specified: The base class "B" cannot be initialized because it does not have a default constructor.

The initialization of a virtual base is done by the most derived object.
Your class D must provide the necessary initializers for the virtual base
B. The virtual base is initialized just once and before any of the

non-virtual bases are initialized.


I'm having another problem (I get a link error when I define a constructor -
it says some symbol is defined more than once), but I can't narrow it down
enough to post it. Anyway, in our code, there is virtual inheritance of the
a class, but no multiple inheritance. Other than designing for the fact
that someone *might* use multiple inheritance, is there any reason to use
virtual inheritance?
Jul 19 '05 #6

"jeffc" <no****@nowhere.com> wrote in message news:3f********@news1.prserv.net...
I'm having another problem (I get a link error when I define a constructor -
it says some symbol is defined more than once), but I can't narrow it down
enough to post it.


Do you have it not-inlined but still in the include file? That will cause the problem.
Jul 19 '05 #7
jeffc wrote:

I'm having another problem (I get a link error when I define a constructor -
it says some symbol is defined more than once), but I can't narrow it down
enough to post it. Anyway, in our code, there is virtual inheritance of the
a class, but no multiple inheritance. Other than designing for the fact
that someone *might* use multiple inheritance, is there any reason to use
virtual inheritance?


Well you only 'need' virtual inheritance if you have
multiple inheritance of classes derived from a common base
and you only want one copy of the base. It brings with it a
whole host of complications, which you're probably best off
avoiding if at all possible. You'd be unwise to add it just
on the off-chance. Stuff will get complicated enough anyway
so why add to it needlessly?
Jul 19 '05 #8

"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f*********************@news.newshosting.com. ..

"jeffc" <no****@nowhere.com> wrote in message news:3f********@news1.prserv.net...
I'm having another problem (I get a link error when I define a constructor - it says some symbol is defined more than once), but I can't narrow it down enough to post it.


Do you have it not-inlined but still in the include file? That will

cause the problem.

Sorry, I said "constructor" but I meant to say "destructor". I do not have
anything defined in the include file. I realize it's not enough to go on,
but until and unless I can narrow down the problem, posting the code isn't
really an option.
Jul 19 '05 #9

"lilburne" <li******@godzilla.net> wrote in message
news:bo*************@ID-203936.news.uni-berlin.de...

Well you only 'need' virtual inheritance if you have
multiple inheritance of classes derived from a common base
and you only want one copy of the base. It brings with it a
whole host of complications, which you're probably best off
avoiding if at all possible. You'd be unwise to add it just
on the off-chance. Stuff will get complicated enough anyway
so why add to it needlessly?


Yes, well that is true. The problem is that it's already there in the code
I'm customizing, and I really don't know why.
Jul 19 '05 #10
jeffc wrote:
"lilburne" <li******@godzilla.net> wrote in message
news:bo*************@ID-203936.news.uni-berlin.de...
Well you only 'need' virtual inheritance if you have
multiple inheritance of classes derived from a common base
and you only want one copy of the base. It brings with it a
whole host of complications, which you're probably best off
avoiding if at all possible. You'd be unwise to add it just
on the off-chance. Stuff will get complicated enough anyway
so why add to it needlessly?

Yes, well that is true. The problem is that it's already there in the code
I'm customizing, and I really don't know why.


Ask the class author, or check back through the classes
implementation history. What other classes inherit from it,
and how do they resolve the problems? Was the virtual really
intended or was someone just trying to impress? Perhaps you
can get permission to remove it.
Jul 19 '05 #11

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

Similar topics

5
by: Jeff Greenberg | last post by:
Not an experienced c++ programmer here and I've gotten myself a bit stuck. I'm trying to implement a class lib and I've run into a sticky problem that I can't solve. I'd appreciate any help that I...
4
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that...
4
by: aap | last post by:
Hi, I have the following code. #include <iostream> using namespace std; class Root { public: virtual void Root1() = 0; virtual void Root2() = 0;
3
by: news.microsoft.com | last post by:
Hi, It is possible to override a non virtual method with the "new" keyword So how is this different from specifying a method as virtual then providing the override keyword? Is there any...
17
by: Student | last post by:
Hi All, I have an assignment for my Programming language project to create a compiler that takes a C++ file as input and translate it into the C file. Here I have to take care of inheritance and...
3
by: Imre | last post by:
Hi! I've got some questions regarding heavy use of virtual inheritance. First, let's see a theoretical situation, where I might feel tempted to use a lot of virtual inheritance. Let's...
5
by: toton | last post by:
Hi, I want a few of my class to overload from a base class, where the base class contains common functionality. This is to avoid repetition of code, and may be reducing amount of code in binary,...
9
by: Chrissy | last post by:
I took a C# class as an elective and received an incomplete in it and am desparate for help. I have two assignments left (arrays and inheritance) and would gladly pay anyone that can assist me with...
23
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
12
by: Massimo | last post by:
Hi to all, I'm facing a problem in a particularly complex inheritance hierarchy, and I'd like to know what the standard says about it and if my compiler is correct in what it does. I have two...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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...
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...

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.