-1
class vehicle{
...
};
class motorVehicle: virtual public vehicle{
...
};
class twoWheels: virtual public vehicle{
...
};
class motorcycle: public motorVehicle, public twoWheels, virtual vehicle{//(1)
....
};

(1) Why does class motorcycle has to inherit class vehicle, when it is already contained in classes motorVehicle and twoWheels? In the explanation from the book it is written that class motorcycle has to inherit class vehicle in order to make sure that the constructor of the base class (vehicle) will be called. If this explanation is correct, I don't understand why it says "virtual vehicle"? What type of inheritance is this?

Kioko Key
  • 119
  • 1
  • 10
  • http://stackoverflow.com/questions/21558/in-c-what-is-a-virtual-base-class (out of sledge hammer duplicate votes for today, sorry) – πάντα ῥεῖ May 03 '15 at 19:44
  • No, it's not the same questions. I don't understand how is inheriting base class connected with constructors. – Kioko Key May 03 '15 at 19:47
  • You don't show any constructors, nor you explain what you don't understand in particular. – πάντα ῥεῖ May 03 '15 at 19:48
  • You should read the question again. It is written that class motorcycle inherits two classes that are derived from class vehicle and also inherits class vehicle again. I am asking whether it has something to do with using the constructor of the base class, because that is explanation from my book. – Kioko Key May 03 '15 at 19:52
  • And inheriting `virtual vehicle` makes it unambiguous, so what? I've read your question dude. – πάντα ῥεῖ May 03 '15 at 19:54
  • I'm not a "dude", I'm a girl. And you could help me, instead of posting comments about how my question is bad. – Kioko Key May 03 '15 at 19:59
  • Sorry I always thought _dude_ applies gender neutral :) (well, in german we'd say _"Alder"(m)_ / _"Alde"(f)_)... I didn't post how your question is bad, but linked another Q&A that merely contains the answer for what you asking about. – πάντα ῥεῖ May 03 '15 at 20:03
  • Ok, we are cool.:) Hm, I read it and still don't understand how is it related to my question. ctrl+f and see if the word "constructor" appears anywhere on that page. – Kioko Key May 03 '15 at 20:07
  • _"Ok, we are cool.:)"_ Of course we are babe :) There's nothing significant that `virtual` inheritance has to do with constructors, besides it's guaranteed that `vehicle` constructor is called only once, though introduced multiple times from other derived classes. – πάντα ῥεῖ May 03 '15 at 20:12
  • So, there is no need to write that part "virtual vehicle"? In case I want to write it, because my professor does so (it is an example from his book), could you please explain me why it only says "virtual vehicle" , instead of for example "virtual public vehicle"? :) – Kioko Key May 03 '15 at 20:20
  • I've supplied and answer. But please take note that googling virtual inheritance will get you a long way there. I've not been able to find a completely identical question. But please rewrite your question so that it shows you know what's going on. People seem to misinterpret your question, and for that there's probably two sides to blame:). – laurisvr May 03 '15 at 20:39

2 Answers2

2

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()
{



}
laurisvr
  • 2,724
  • 6
  • 25
  • 44
1

This code

class motorcycle: public motorVehicle, public twoWheels, virtual vehicle{//(1)
....
};

inherits your class from both motorVehicle and twoWheels, which both already inherit vehicle. Thus you have to disambiguate the construction of vehicle, and adding virtual vehicle to the inheritance hierarchy, does so by choosing the first available constructors called by any other inherited class.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190