In many textbooks describing the use of virtual base classes (usually used to solve the problem with diamonds), they often have code similar to the design of this structure:
class Animal { public: Animal() { cout << "Creating Animal\n"; } }; /////////////////////////// class FourLegs : virtual public Animal { public: FourLegs() { cout << "Creating FourLegs\n"; } }; /////////////////////////// class Mammal : virtual public Animal { public: Mammal() { cout << "Creating Mammal\n"; } }; /////////////////////////// class Fox : public FourLegs, public Mammal { public: Fox() { cout << "Creating Fox\n"; } };
When I create an instance of Fox, I get the expected result, only one created by Animal:
Creating Animal Creating FourLegs Creating Mammal Creating Fox
As you can see, I have two level 2 classes, inherited practically. Now, if only the class one tier-2 is inherited practically, and the other is inherited simply publicly, interesting results can arise. For example, if FourLegs is inherited public and Mammal is inherited by the virtual public, this is the result:
Creating Animal Creating Animal Creating FourLegs Creating Mammal Creating Fox
This is strange and begs the question: What is the complete process of creating a class that includes virtual inheritance somewhere in the inheritance tree?
On the other hand, if I FourLegs, if I inherited virtual publishing, and Mammal is inherited publicly, then the output will be just as normal (as if nothing had been inherited by virtual public):
Creating Animal Creating FourLegs Creating Animal Creating Mammal Creating Fox