J
JKop
I'm starting to think that whenever you derive one class from another, that
you should use virtual inheritance *all* the time, unless you have an
explicit reason not to. I'm even thinking that there shouldn't have been a
"virtual" keyword for this purpose, but instead, a "nonvirtual" keyword!
In teaching inheritance, you see the common example:
class Vehicle {}
class Car : public Vehicle {}
class Boat : public Vehicle {}
But in my opinion, this *most basic* example should be:
class Vehicle {}
class Car : virtual public Vehicle {}
class Boat : virtual public Vehicle {}
I'm thinking that one should stick in virtual inheritance *everywhere*,
unless there's an explicit reason not to. Here's one reason why:
class Vehicle {}
class Car : virtual public Vehicle {}
class Boat : virtual public Vehicle {}
class CarBoat : virtual public Car, virtual public Boat {}
Here we can see that it's preferrable to use virtual inheritance
*everywhere* unless you've an explicit reason not to.
Even going further:
class Vehicle {}
class Vehicle_Land : virtual public Vehicle {}
class Vehicle_Water : virtual public Vehicle {}
class Vehicle_Air : virtual public Vehicle {}
class Vehicle_Space : virtual public Vehicle {}
class Car : virtual public Vehicle_Land {}
class Motorcycle : virtual public Vehicle_Land {}
class Boat : virtual public Vehicle_Water {}
class Plane : virtual public Vehicle_Air {}
class Helicopter : virtual public Vehicle_Air {}
class SpaceShuttle : virtual public Vehicle_Space {}
class CarBoat : virtual public Car, virtual public Boat {}
class HelicopterMotorcycle : virtual public Helicopter, virtual public
Motorcycle{}
class UltimateVehicle : virtual public HelicopterMotorcycle, virtual public
SpaceShuttle {}
Now... "UltimateVehicle" looks like so:
-----------
| Vehicle |
-----------
^ ^ ^
___________| | |______________
/ | \
/ | \
| | |
| | |
---------------- --------------- -----------------
| Vehicle_Land | | Vehicle_Air | | Vehicle_Space |
---------------- --------------- -----------------
^ ^ ^
| | |
| | |
-------------- -------------- ----------------
| Motorcycle | | Helicopter | | SpaceShuttle |
-------------- -------------- ----------------
^ ^ ^
| | |
\ / /
\ / /
\ / /
\ / /
\ / /
------------------------ /
| MotorcycleHelicopter | /
------------------------ /
^ /
| /
\ /
\ /
\ /
\ /
\ /
-------------------
| UltimateVehicle |
-------------------
And this is exactly what we want. We've not explicit reason to make *any* of
the inheritances non-virtual.
Any thoughts on this?
One more thing, why is it called "virtual" inheritanc? I see no similarity
whatsoever between *it* and the "virtual" keyword applied to a function.
-JKop
you should use virtual inheritance *all* the time, unless you have an
explicit reason not to. I'm even thinking that there shouldn't have been a
"virtual" keyword for this purpose, but instead, a "nonvirtual" keyword!
In teaching inheritance, you see the common example:
class Vehicle {}
class Car : public Vehicle {}
class Boat : public Vehicle {}
But in my opinion, this *most basic* example should be:
class Vehicle {}
class Car : virtual public Vehicle {}
class Boat : virtual public Vehicle {}
I'm thinking that one should stick in virtual inheritance *everywhere*,
unless there's an explicit reason not to. Here's one reason why:
class Vehicle {}
class Car : virtual public Vehicle {}
class Boat : virtual public Vehicle {}
class CarBoat : virtual public Car, virtual public Boat {}
Here we can see that it's preferrable to use virtual inheritance
*everywhere* unless you've an explicit reason not to.
Even going further:
class Vehicle {}
class Vehicle_Land : virtual public Vehicle {}
class Vehicle_Water : virtual public Vehicle {}
class Vehicle_Air : virtual public Vehicle {}
class Vehicle_Space : virtual public Vehicle {}
class Car : virtual public Vehicle_Land {}
class Motorcycle : virtual public Vehicle_Land {}
class Boat : virtual public Vehicle_Water {}
class Plane : virtual public Vehicle_Air {}
class Helicopter : virtual public Vehicle_Air {}
class SpaceShuttle : virtual public Vehicle_Space {}
class CarBoat : virtual public Car, virtual public Boat {}
class HelicopterMotorcycle : virtual public Helicopter, virtual public
Motorcycle{}
class UltimateVehicle : virtual public HelicopterMotorcycle, virtual public
SpaceShuttle {}
Now... "UltimateVehicle" looks like so:
-----------
| Vehicle |
-----------
^ ^ ^
___________| | |______________
/ | \
/ | \
| | |
| | |
---------------- --------------- -----------------
| Vehicle_Land | | Vehicle_Air | | Vehicle_Space |
---------------- --------------- -----------------
^ ^ ^
| | |
| | |
-------------- -------------- ----------------
| Motorcycle | | Helicopter | | SpaceShuttle |
-------------- -------------- ----------------
^ ^ ^
| | |
\ / /
\ / /
\ / /
\ / /
\ / /
------------------------ /
| MotorcycleHelicopter | /
------------------------ /
^ /
| /
\ /
\ /
\ /
\ /
\ /
-------------------
| UltimateVehicle |
-------------------
And this is exactly what we want. We've not explicit reason to make *any* of
the inheritances non-virtual.
Any thoughts on this?
One more thing, why is it called "virtual" inheritanc? I see no similarity
whatsoever between *it* and the "virtual" keyword applied to a function.
-JKop