Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You cannot override them separately, you must override both at once:</p> <pre><code>struct Interface1 { virtual void Name() = 0; }; struct Interface2 { virtual void Name() = 0; }; struct RealClass : Interface1, Interface2 { virtual void Name(); }; // and move it out of the class definition just like any other method: void RealClass::Name() { printf("Interface1 OK?\n"); printf("Interface2 OK?\n"); } </code></pre> <p>You can simulate individual overriding with intermediate base classes:</p> <pre><code>struct RealClass1 : Interface1 { virtual void Name() { printf("Interface1 OK?\n"); } }; struct RealClass2 : Interface2 { virtual void Name() { printf("Interface2 OK?\n"); } }; struct RealClass : RealClass1, RealClass2 { virtual void Name() { // you must still decide what to do here, which is likely calling both: RealClass1::Name(); RealClass2::Name(); // or doing something else entirely // but note: this is the function which will be called in all cases // of *virtual dispatch* (for instances of this class), as it is the // final overrider, the above separate definition is merely // code-organization convenience } }; </code></pre> <p>Additionally, you're using reinterpret_cast incorrectly, you should have:</p> <pre><code>int main() { RealClass rc; // no need for dynamic allocation in this example Interface1&amp; one = rc; one.Name(); Interface2&amp; two = dynamic_cast&lt;Interface2&amp;&gt;(one); two.Name(); return 0; } </code></pre> <p>And here's a rewrite with <a href="http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern" rel="nofollow noreferrer">CRTP</a> that might be what you want (or not):</p> <pre><code>template&lt;class Derived&gt; struct RealClass1 : Interface1 { #define self (*static_cast&lt;Derived*&gt;(this)) virtual void Name() { printf("Interface1 for %s\n", self.name.c_str()); } #undef self }; template&lt;class Derived&gt; struct RealClass2 : Interface2 { #define self (*static_cast&lt;Derived*&gt;(this)) virtual void Name() { printf("Interface2 for %s\n", self.name.c_str()); } #undef self }; struct RealClass : RealClass1&lt;RealClass&gt;, RealClass2&lt;RealClass&gt; { std::string name; RealClass() : name("real code would have members you need to access") {} }; </code></pre> <p>But note that here you cannot call Name on a RealClass now (with virtual dispatch, e.g. <code>rc.Name()</code>), you must first select a base. The self macro is an easy way to clean up CRTP casts (usually member access is much more common in the CRTP base), but it can be <a href="http://bitbucket.org/kniht/scraps/src/tip/cpp/crtp.hpp" rel="nofollow noreferrer">improved</a>. There's a brief discussion of virtual dispatch in one of my <a href="https://stackoverflow.com/questions/1963926/when-is-a-v-table-created-in-c/1963977#1963977">other answers</a>, but surely a better one around if someone has a link.</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. 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.
 

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