Note that there are some explanatory texts on larger screens.

plurals
  1. PODistinct implementations for pure virtual functions with same name
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/2004820/inherit-interfaces-which-share-a-method-name">Inherit interfaces which share a method name</a> </p> </blockquote> <p>I have two base classes <code>I1</code> and <code>I2</code> with pure virtual functions <code>void R() = 0;</code>. I want the derived class <code>IImpl</code> to inherit from <code>I1</code> and <code>I2</code> and to have distinct implementations for <code>I1::R()</code> and <code>I2::R()</code>.</p> <p>The code below compiles and works in MS VS 2005 and 2010. I compile with Language Extension disabled and on warning level 4. There are no warnings and no errors.</p> <p>I tried the same code in gcc 4.2. It does not compile. GCC reports an error:</p> <pre><code>error: cannot define member function 'I1::R' within 'IImpl' </code></pre> <p>My questions are: </p> <ol> <li>Why that code works in MS VS and why it does not work in gcc?</li> <li>Is the code a standard C++? </li> <li>What is the correct way to implement it, so it is a standard C++ and compiles on VS and gcc?</li> </ol> <p>Thanks!</p> <pre><code>#include &lt;stdio.h&gt; class I1 { public: virtual void R() = 0; virtual ~I1(){} }; class I2 { public: virtual void R() = 0; virtual ~I2(){} }; class IImpl: public I1, public I2 { public: virtual void I1::R() { printf("I1::R()\r\n"); } virtual void I2::R() { printf("I2::R()\r\n"); } }; int main(int argc, char* argv[]) { IImpl impl; I1 *p1 = &amp;impl; I2 *p2 = &amp;impl; p1-&gt;R(); p2-&gt;R(); return 0; } </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.
 

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