-
February 22nd, 2009, 01:06 PM
#1
Virtual inheritance and constructors
So I have three classes:
Code:
class Base {
int i;
public:
Base(int value) : i(value) {}
};
class Derived : virtual public Base {
public:
Derived(int value) : Base(value) {}
};
class VeryDerived : public Derived {
VeryDerived(int value) : Derived(value) {}
};
I think I misunderstand how virtual inheritance and c++ constructors interact. This all seems to be reasonable, but my compiler tells me that VeryDerived cannot compile because there's no default Base constructor. None of the articles I've found online seem to mention this issue. Can anybody clarify my misunderstanding?
EDIT: by the way, I understand that in this limited example there's no need for virtual inheritance, but I am boiling down an issue that I'm having with something that I intend to be multiply-inherited. In my real program, I want to both make a non-multiply-inherited subclass of Derived and a multiply-inherited subclass of Derived.
Last edited by Rossman231; February 22nd, 2009 at 01:09 PM.
Reason: Clarification.
-
February 22nd, 2009, 01:51 PM
#2
Re: Virtual inheritance and constructors
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
-
February 22nd, 2009, 02:27 PM
#3
Re: Virtual inheritance and constructors
With virtual inheritance, the most-derived class is responsible for constructing the virtual bases. Since you didn't explicitly call the Base(int) ctor, it's trying to find a Base(void) ctor, and none exists.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|