You might want to fix the subject of your thread to
virtual inheritance instead of
multiple inheritance. Your question is really about virtual inheritance. You would have this issue with virtual inheritance even without multiple inheritance (which would be pointless, but you can do it). You wouldn't have this issue with multiple inheritance without virtual.
Quote:
Originally Posted by vbx_wx
How can I fix the code so I no longer need to call X(1) in the initialization list of constructor A(int) ?
|
In the sense that I think you mean that,
you can't.
That is one of the many problems with virtual inheritance. If the virtual base class doesn't have a default constructor, every derived class must explicitly invoke the constructor of that virtual base class. If it had a default constructor but you happen to want to use a non default constructor, then only each most derived class instantiated needs to invoke the virtual base class constructor.
But you want to avoid the constructor in the most derived class, not in the intermediate classes. I'm pretty sure you can't.
The virtual base class constructor specified by the class actually instantiated is the one that actually is executed.
In your example, when you instantiate an A, the X constructor specified by the A constructor is the only X constructor executed. The X constructors specified by the Y and Z constructors are ignored. Those would be used only if you instantiate a Y or Z object, not when you instantiate any object derived from Y or Z.
Quote:
Originally Posted by vbx_wx
How can I fix the code so I no longer need to call X(1) in the initialization list of constructor A(int) ?
|
A more literal reading of your question (I'm pretty sure violating your intent) would have the answer: Define a default constructor for the virtual base class and let that be the constructor implicitly used by A.