So I have this idea and I think it's basically impossible to implement in C++... but I want to ask. I read through chapter 15 of Stroustrup and didn't get my answer, and I don't think the billion other questions about inheritance diamonds answer this one, so I'm asking here.
The question is, what happens when you inherit from two base classes which share a common base class themselves, but only one of the two inherits from it virtually. For example:
class CommonBase { ... };
class BaseA : CommonBase { ... };
class BaseB : virtual CommonBase { ... };
class Derived : BaseA, BaseB { ... };
The reason I think I want to do this is because I'm trying to extend an existing library without having to recompile the whole library (don't want to open that can of worms). There already exists a chain of inheritance that I would like to modify. Basically something like this (excuse the ascii art)
LibBase
| \
| \
| MyBase
| |
| |
LibDerived |
| \ |
| \ |
| MyDerived
| |
LibDerived2 |
| \ |
| \ |
| MyDerived2
| |
LibDerived3 |
| \ |
| \ |
| MyDerived3
| |
LibConcrete |
\ |
MyConcrete
Get the picture? I want an object of each of "My
" classes to be an object of the class they are essentially replacing, but I want the next class in the inheritence diagram to use the overridden method implementation from "My
" base class, but all the other methods from the library's classes. The library classes do not inherit virtually so it's like this
class LibDerived : LibBase
But if I make my class inherit virtually
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?
I hope this question is clear enough.