Note that there are some explanatory texts on larger screens.

plurals
  1. POAmbiguity in multiple inheritance of interfaces in C++
    text
    copied!<p>I made a test code as following:</p> <pre class="lang-C++ prettyprint-override"><code>#include &lt;iostream&gt; using namespace std; #ifndef interface #define interface struct #endif interface Base { virtual void funcBase() = 0; }; interface Derived1 : public Base { virtual void funcDerived1() = 0; }; interface Derived2 : public Base { virtual void funcDerived2() = 0; }; interface DDerived : public Derived1, public Derived2 { virtual void funcDDerived() = 0; }; class Implementation : public DDerived { public: void funcBase() { cout &lt;&lt; "base" &lt;&lt; endl; } void funcDerived1() { cout &lt;&lt; "derived1" &lt;&lt; endl; } void funcDerived2() { cout &lt;&lt; "derived2" &lt;&lt; endl; } void funcDDerived() { cout &lt;&lt; "dderived" &lt;&lt; endl; } }; int main() { DDerived *pObject = new Implementation; pObject-&gt;funcBase(); return 0; } </code></pre> <p>The reason I wrote this code is to test if the function funcBase() can be called in an instance of DDerived or not. My C++ complier (Visual Studio 2010) gave me a compile error message when I tried to compile this code. In my opinion, there is no problem in this code because it is certain that the function <code>funcBase()</code> will be implemented (thus overriden) in some derived class of the interface <code>DDerived</code>, because it is pure virtual. In other words, any pointer variable of type <code>Implementation *</code> should be associated with an instance of a class deriving Implentation and overriding the function <code>funcBase()</code>. </p> <p>My question is, why the compiler give me such an error message? Why the C++ syntax is defined like that; i.e., to treat this case as an error? How can I make the code runs? I want to allow multiple inheritance of interfaces. Of course, if I use "virtual public" or re-declare the function <code>funcBase()</code> in <code>Implementation</code> like </p> <pre class="lang-C++ prettyprint-override"><code>interface DDerived : public Derived1, public Derived2 { virtual void funcBase() = 0; virtual void funcDDerived() = 0; }; </code></pre> <p>then everything runs with no problem. </p> <p>But I don't want to do that and looking for more convenient method, because virtual inheritance may degrade the performance, and re-declaration is so tedious to do if inheritance relations of classes are very complex. Is there any methods to enable multiple inheritance of interfaces in C++ other than using virtual inheritance?</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