Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Okay, it is possible, and the way isn't too ugly. I have to add an additional level of inheritance:</p> <pre><code> ITestA ITestB &lt;-- These are the interfaces C has to fulfill, both with test() | | ITestA_X ITestB_X &lt;-- These classes implement the interface by calling a | | function with a different name, like ITestA_test \__________/ which is in turn pure virtual again. | C &lt;-- C implements the new interfaces </code></pre> <p>Now C has no function <code>test()</code>, but when casting a <code>C*</code> to an <code>ITestA*</code>, the implementation of <code>test()</code> in <code>ITestA_test</code> will be used. When casting it to an <code>ITestB*</code>, even by a dynamic_cast from the <code>ITestA*</code>, the implementation of <code>ITestB_test</code> will be used. The following program prints: 3.14 0</p> <pre><code>#include &lt;iostream&gt; class ITestA { public: virtual ~ITestA() {}; virtual float test() =0; }; class ITestB { public: virtual ~ITestB() {}; virtual bool test() =0; }; class ITestA_X : public ITestA { protected: virtual float ITestA_test() =0; virtual float test() { return ITestA_test(); } }; class ITestB_X : public ITestB { protected: virtual bool ITestB_test() =0; virtual bool test() { return ITestB_test(); } }; class C : public ITestA_X, public ITestB_X { private: virtual float ITestA_test() { return 3.14; } virtual bool ITestB_test() { return false; } }; int main() { ITestA *a = new C(); std::cout &lt;&lt; a-&gt;test() &lt;&lt; std::endl; ITestB *b = dynamic_cast&lt;ITestB *&gt;(a); if (b) { std::cout &lt;&lt; b-&gt;test() &lt;&lt; std::endl; } delete(a); return 0; } </code></pre> <p>Does this have any drawbacks you could think of?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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