So, I have this idea, and I think it is almost impossible to implement in C ++ ... but I want to ask. I read chapter 15 of the Stroustrup and didnβt get an answer, and I donβt think that we will answer a billion other questions about inheritance diamonds, so I ask here.
The question is what happens when you inherit from two base classes that share a common base class, but only one of the two inherits from it in practice. For instance:
class CommonBase { ... }; class BaseA : CommonBase { ... }; class BaseB : virtual CommonBase { ... }; class Derived : BaseA, BaseB { ... };
I think I want to do this because I am trying to expand an existing library without recompiling the entire library (I do not want to open this worm from worms). There is already an inheritance chain that I would like to change. Basically something like this (sorry ascii art)
LibBase | \ | \ | MyBase | | | | LibDerived | | \ | | \ | | MyDerived | | LibDerived2 | | \ | | \ | | MyDerived2 | | LibDerived3 | | \ | | \ | | MyDerived3 | | LibConcrete | \ | MyConcrete
Get an image? I want the object of each class " My " to be an object of the class, which they essentially replace, but I want the next class in the inheritance diagram to use the implementation of the overridden method from the base class " My ", but also all other methods from the library classes. Library classes do not inherit almost as it looks
class LibDerived : LibBase
But if I make my class inheritable practically
class MyBase : virtual LibBase {}; class MyDerived: virtual MyBase, virtual LibDerived {};
Since MyDerived will have a vtable and MyBase will have a vtable, will there be only one LibBase object?
Hope this question is clear enough.