First of all, the solution to the problem given, surely is a solution but in my humble opinion not the best.
This is to solve the so called diamond inheritance problem. For this it's important to understand virtual inheritance and the difference between public and private inheritance.
Public inheritance
means the derived class can also be considered as and casted to the inherited class. In your particular case a motor vehicle
can be casted to a vehicle
. Had the inheritance been private. This would not have been possible.
So the same applies again to motorcycle
. It can be considered twoWheels
and to motorVehicle
. But since both of those can be considered a vehicle a problem arises. What if you want to cast the motorCycle
to a vehicle
. Should it first cast to twoWheels
, and then to vehicle
. Or it first to motorVehicle
and then to vehicle
.
And that's not all, any variable or function declared in vehicle, can also be called in motorcycle
. But should it call those of twoWheels
and then to motorVehicle
.
So in essence there are two vehicles
being constructed and this creates an ambiguity. To solve this virtual inheritance is introduced. This allows you to initialize any class in the inheritance chain yourself. For this it's not necessary to inherit from the initialized class yourself.
And that's why i don't agree with the solution presented. It would be sufficient to write the motorcycle
constructor like this:
motorcycle::motorcycle():
vehicle(),//n.b. this order is Not relevant, vehicle will be constructed first. No matter the order in this list.
twoWheels(),
motorVehicle()
{
}