+ 21

When you should use virtual inheritance?

13th Dec 2016, 8:53 PM
//Lana Wilson
//Lana Wilson - avatar
4 Antworten
+ 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.
13th Dec 2016, 10:19 PM
Antek
+ 4
in case of hybrid inheritance when u derive a class from two classes which are originally derived from a common class
14th Dec 2016, 7:08 AM
Vijay Chawla
Vijay Chawla - avatar
0
i can not program writing
14th Dec 2016, 12:32 AM
medov
medov - avatar
- 1
Lana Wilson pls contact me via Facebook, or Skype
7th Jan 2017, 10:55 AM
calms
calms - avatar