+ 21
When you should use virtual inheritance?
5 Réponses
+ 6
It is helpful when you want to inherit virtual functions through multiple levels of inheritance, and the lowest derived class inherits its base class through multiple classes.
For instance, you could have a base class Animal that has two derived classes Mammal and WingedAnimal, which both inherit from Animal directly. Below this level, a derived class Bat could inherit from both Mammal and WingedAnimal directly. Unfortunately, this leads to two separate references to Animal as Bat now inherits from Animal twice through Mammal and WingedAnimal. This is called Diamond inheritance and it is a problem due to the ambiguity of whether or not Bat should inherit Mammal:Animal or WingedAnimal:Animal. Bat gets two separate sets of Animal data.
Declaring the middle classes as virtual helps eliminate the ambiguity and makes sure that the Bat class has only one reference to Animal. I took this example from the wiki page on Virtual Inheritance which you can reference for more detail.
+ 4
in case of hybrid inheritance when u derive a class from two classes which are originally derived from a common class
0
i can not program writing
- 1
Lana Wilson pls contact me via Facebook, or Skype