Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiple polymorphism in C++
    primarykey
    data
    text
    <p>There's the compile error " ' A ' is an ambiguous base of ' C ' " existing on the test <code>diamond multiple inheritance</code> below,</p> <pre><code>struct A { void f();}; struct B1: A {}; struct B2: A {}; struct C: B1, B2 {}; void g() {A *o = new C;} </code></pre> <p>It's solved by <code>virtual inheritance</code> below,</p> <pre><code>struct B1: virtual A {}; struct B2: virtual A {}; </code></pre> <p>Now there's the compile error " no unique final overridder for ' virtual void A::f() ' in ' C ' " existing on another test <code>diamond multiple polymorphism</code> below,</p> <pre><code>struct A {virtual void f();}; struct B1: virtual A {void f();}; struct B2: virtual A {void f();}; struct C: B1, B2 {}; void g() { A *o = new C; o-&gt;f(); } </code></pre> <p>And it can't be solved even by <code>dynamic_cast</code> below,</p> <pre><code>dynamic_cast&lt;B1 *&gt;(o)-&gt;f(); </code></pre> <p><strong>@NOTE</strong></p> <p><code>dynamic_cast&lt;T *&gt;(obj_ptr)</code> is actually used to perform <code>typesafe downcast</code>, i.e. when the run-time type Tobj of the object that the obj_ptr points to is a subtype of T, it returns obj_ptr itself; otherwise the NULL pointer. It is mistaken being thought of upcasting the subtype Tobj to the supertype T, otherwise performing upcast at run-time directly contradicts the principle of polymorphism.</p> <p>IMHO, <code>virtual inheritance</code> is succinct enough to solve the first-half problem at compile-time. In contrast, can you present anything to solve the second-half problem at run-time?</p> <p><strong>@EDIT</strong></p> <p>Thanks for your pointing out <code>dynamic_cast</code> doesn't do the job. Corrected.</p> <p>To sovle the 2nd-half problem, it seems to have no choice but implement the overrider on the final subclass of the diamond hierarchy below,</p> <pre><code>struct C: B1, B2 { void f() {B1::f();} //Hardcode to your choice of overrider }; </code></pre>
    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.
 

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