Note that there are some explanatory texts on larger screens.

plurals
  1. POInterface-like inheritance in C++
    text
    copied!<p>I have the following situation, pictured is the theoretical inheritance graph of my classes:</p> <p><img src="https://i.stack.imgur.com/jT9rk.png" alt="Inheritance Graph"></p> <p>The idea is basically to</p> <p>1) have two abstract base classes that can be implemented on different platforms (in my case two different operating systems)</p> <p>2) allow BBase to be up-castable to ABase to be able to handle both equally at times (e.g. to hold instances of both types in one list).</p> <p>3) implement certain common functionality in ABase and BBase.</p> <p>Now, what would be the best way to represent this in C++? Even though it does support multiple inheritance, multi-level inheritence like this is not possible to my knowledge. The problem is that B inherits from A and BBase, which both in turn inherit from ABase. Simply translating this 1:1 (following code) in C++, a C++ compiler (GNU) will complain that ABase::foo() is not implemented in B.</p> <pre><code>class ABase { public: virtual void foo() = 0; void someImplementedMethod() {} }; class BBase : public ABase { public: virtual void bar() = 0; void someOtherImplementedMethod() {} }; class A : public ABase { public: A() {} void foo() {} }; class B : public A, public BBase { public: B() : A() {} void bar() {} }; int main() { B b; return 0; } </code></pre> <p>How would you change this inheritance model to make it compatible to C++?</p> <p><strong>EDIT:</strong> Inverted arrows in diagram and corrected "down-castable" to "up-castable".</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