Note that there are some explanatory texts on larger screens.

plurals
  1. POInherit interfaces which share a method name
    primarykey
    data
    text
    <p>There are two base classes have same function name. I want to inherit both of them, and over ride each method differently. How can I do that with separate declaration and definition (instead of defining in the class definition)?</p> <pre><code>#include &lt;cstdio&gt; class Interface1{ public: virtual void Name() = 0; }; class Interface2 { public: virtual void Name() = 0; }; class RealClass: public Interface1, public Interface2 { public: virtual void Interface1::Name() { printf("Interface1 OK?\n"); } virtual void Interface2::Name() { printf("Interface2 OK?\n"); } }; int main() { Interface1 *p = new RealClass(); p-&gt;Name(); Interface2 *q = reinterpret_cast&lt;RealClass*&gt;(p); q-&gt;Name(); } </code></pre> <p>I failed to move the definition out in VC8. I found the Microsoft Specific Keyword __interface can do this job successfully, code below:</p> <pre><code>#include &lt;cstdio&gt; __interface Interface1{ virtual void Name() = 0; }; __interface Interface2 { virtual void Name() = 0; }; class RealClass: public Interface1, public Interface2 { public: virtual void Interface1::Name(); virtual void Interface2::Name(); }; void RealClass::Interface1::Name() { printf("Interface1 OK?\n"); } void RealClass::Interface2::Name() { printf("Interface2 OK?\n"); } int main() { Interface1 *p = new RealClass(); p-&gt;Name(); Interface2 *q = reinterpret_cast&lt;RealClass*&gt;(p); q-&gt;Name(); } </code></pre> <p>but is there another way to do this something more general that will work in other compilers?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    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