views:

160

answers:

2

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.

+2  A: 

Essentially, you are right. You need to have LibDerived derived virtually from LibBase if you want this sort of inheritance tree to work.

If you don't have this you can't prevent having a non-virtual LibBase under the LibDerived and a separate virtual LibBase under MyBase.

Charles Bailey
So every non-virtual inheritance link in the hierarchy will introduce a new base-object, and every virtual inheritance link will be compacted into a single, other, base-object?
cheshirekow
Exactly. You have one virtual base class instance for _all_ the times a base class appears as a virtual base in the hierarchy *plus* one base class instances for _each_ time a class appears as a non-virtual base class in the hierarchy.
Charles Bailey
+1  A: 

To simplify answer let's think about virtual/non-virtual as duplicated or non-duplicated content.

class LibDerived : LibBase

declares: I allow LibBase be twice (or more ) entered into descending of LibDerived

class MyBase : virtual LibBase {};

declares: I allow compiler to optimize two entries of LibBase in MyBase descendings into single one.

When these two declarations meet each one, the first is more priority so MyDerived gets 2 implmentation of LibBase. But power of c++ is possibility to resolve it! Just make overriding on MyDerived virtual functions to select which you want to use. Ore another way - create universal wrapper of MyDerived derived from interface LibBase that aggregate any instance: LibDerived, MyBase, ... and call expected method from aggregate.

Dewfy
Can you explain further the resolution you mentioned? When you say overriding on MyDerived virtual functions to select the ones I want, do you mean just "accept the fact that you have two LibBase objects, and override the methods to expose only one of them"? Unfortunately I don't think the universal wrapper would do what I would like because it would then not be compatible with the rest of the library. I'd like the each of the derived class objects to "appear" as if it's a native object of the library, but simply have a different implementation for some of the methods.
cheshirekow
`virtual` in heritance is not for allowing a compiler to optimize out base classes. A `virtual` base class is for guaranteeing that there is only one base class sub-object no matter how many times that type appears as a virtual base class in the inheritance tree of a derived class object. A non-virtual base class is guaranteed to add a new base class sub-object. There's no priority and there's no option to add a second virtual base for the same type.
Charles Bailey
Cheshirekow, let me answer in back order.Aggregate exactly allows to choose between multiple implementation, you just make wrapper around impl. And MyConcrete, for example at constructor time, accepts pointer to expected implementation and exposes it by delagating each call to aggreate."accept the fact that you have two LibBase" - unfortunately "yes" nonvirtual declaration grants you manually need override each virtual function to make decision which member of two copies must be invoked.
Dewfy