Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>First solution</strong></p> <p>This question remind the 'facade' design pattern. This should be re-write as this :</p> <pre><code>class AC : public A { public: virtual void f(){ cout &lt;&lt; "f() from A" &lt;&lt; endl;}; }; class BC : public B ... class C : public AC, public BC {}; </code></pre> <p>where C is the 'facade'.</p> <p>So in the correct calling syntax should be something like that :</p> <pre><code>C* c = new C(); c-&gt;AC::f(); c-&gt;BC::f(); </code></pre> <p>If you don't have any share constraint between AC &amp; BC this should do the job as it is'nt offuscated.</p> <p><strong>Second solution</strong></p> <p>Another solution, <em>thank to Casey</em> (see first comment), is to use a forward declaration of the class C in a template to allow calls to methods define latter.</p> <pre><code>template &lt;typename C&gt; class AC : public A { public: void f() { static_cast&lt;C&amp;&gt;(*this).f_from_A(); } }; template &lt;typename C&gt; class BC : public B { ... }; </code></pre> <p>so the implementation part can be done in the same class.</p> <pre><code>class C : public AC&lt;C&gt;, public BC&lt;C&gt; { public: void f_from_A() { cout &lt;&lt; "f_from_A" &lt;&lt; endl; }; void f_from_B() ... }; </code></pre> <p>The calling part is cleaner because <em>it doesn't show any implementation details</em> and <em>it is closest to the question</em> :</p> <pre><code>C* c = new C(); ((A*) c) -&gt; f(); ((B*) c) -&gt; f(); </code></pre> <p>There is no more 'default' f() on C and it is possible to break the expected behavior of inheritance, and it is harder to read.</p>
    singulars
    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.
    3. VO
      singulars
      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