Re: constructor for 'E' must explicitly initialize the base class 'C' which does not have a default constructor
Re: constructor for 'E' must explicitly initialize the base class 'C' which does not have a default constructor
- Subject: Re: constructor for 'E' must explicitly initialize the base class 'C' which does not have a default constructor
- From: John McCall <email@hidden>
- Date: Fri, 18 Jan 2013 02:18:52 -0800
On Jan 18, 2013, at 1:28 AM, Rick Mann <email@hidden> wrote:
> Can anyone explain this error? It sure looks to me like all the base class constructors are being explicitly called.
>
> http://pastebin.com/cVMmgqCZ
This is off-topic for this list, but since I'm here anyway, I'll answer. In the future, though, please take this to a venue that's more specific to C++.
In C++, a constructor must directly initialize all of the virtual bases of the class it appears in; it cannot "inherit" initializers of virtual bases from its base-class constructors. In turn, since all the virtual bases were already initialized by the most-derived class, those initializers are ignored when constructing base classes.
There are many reasons for this which could fill, if not a book, at least a small pamphlet; suffice it to say that that's how it is.
And yes, this does make virtual bases without public default constructors very annoying to use. (This is actually sometimes used as a hacky way to disable or limit subclassing: first, add an empty virtual base with a private default constructor; next, have that class befriend every class that you wish to allow to subclass your type. If your class had a vtable anyway, this doesn't even bloat your object size, although it does make your vtables a bit bigger.)
I believe that most expert C++ programmers would say that virtual inheritance is something that you should use very carefully, if you really must use it at all. Using it pervasively as your standard method of inheritance is almost certainly a sign of a poor class design.
John.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden