Virtual inheritance |
In object-oriented programing languages, virtual inheritance solves some of the problems caused by multiple inheritance by having common grandparent classes used only once. It usually is used when the inheritance is representing restrictions of a set rather than composition of parts.
In C plus plus, virtual inheritance is indicated by the virtual keyword. For example:
class Animal { virtual void Eat(); }; // Two classes virtually inheriting TheCommonBase: class Mammal : public virtual Animal { virtual const Color GetHairColor() const; }; class WingedAnimal : public virtual Animal { virtual void Flap(); }; // A bat can Eat class Bat : public Mammal, public WingedAnimal {};
In the above, if Mammal and WingedAnimal did not inherit virtually, a call to Bat::Eat() would be ambiguous and hence a compile error.|
|