简体   繁体   中英

Asymmetric virtual Inheritance diamond in C++

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.

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM