Hello,
I have a few questions about virtual inheritance. When inheriting from a base class virtually, only one base class will be created to be shared with all the base class's direct-children classes.
First off, I am working with this code (using a pastebin site for neatness):
http://ideone.com/j7ncq
In this code, I follow the "diamond" problem format, and use virtual inheritance to create only Animal only once. The output appeared as expected:
Creating Animal
Creating FourLegs
Creating Mammal
Creating Fox
|
Now, I was curious about what would happen if only
one child class inherited virtually, while the other inherited normally.
This time, I inherited FourLegs virtually and Mammal regularly.
http://ideone.com/xyEMG
The result was the same as if their was no virtual inheritance in the first place:
Creating Animal
Creating FourLegs
Creating Animal
Creating Mammal
Creating Fox
|
Now, I tried it the other way around. I inherited FourLegs normally and Mammal virtually.
http://ideone.com/WBFdF
This was the result that surprised me most and have no idea on how it came to be.
Creating Animal
Creating Animal
Creating FourLegs
Creating Mammal
Creating Fox
|
Could somebody mind explaining to me how these results came to be?