Note that there are some explanatory texts on larger screens.

plurals
  1. POAsymmetric virtual Inheritance diamond in C++
    text
    copied!<p>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.</p> <p>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:</p> <pre><code>class CommonBase { ... }; class BaseA : CommonBase { ... }; class BaseB : virtual CommonBase { ... }; class Derived : BaseA, BaseB { ... }; </code></pre> <p>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)</p> <pre><code> LibBase | \ | \ | MyBase | | | | LibDerived | | \ | | \ | | MyDerived | | LibDerived2 | | \ | | \ | | MyDerived2 | | LibDerived3 | | \ | | \ | | MyDerived3 | | LibConcrete | \ | MyConcrete </code></pre> <p>Get the picture? I want an object of each of "<code>My</code>" classes to <em>be</em> 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 "<code>My</code>" base class, but all the other methods from the library's classes. The library classes do not inherit virtually so it's like this</p> <pre><code>class LibDerived : LibBase </code></pre> <p>But if I make my class inherit virtually </p> <pre><code>class MyBase : virtual LibBase {}; class MyDerived: virtual MyBase, virtual LibDerived {}; </code></pre> <p>Since <code>MyDerived</code> will have a vtable, and <code>MyBase</code> will have a vtable, will there be only one <code>LibBase</code> object? </p> <p>I hope this question is clear enough.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload