Note that there are some explanatory texts on larger screens.

plurals
  1. POUnderstanding (simple?) C++ Inheritance
    primarykey
    data
    text
    <p>I'm struggling a bit to understand why this code snippet does not compile.</p> <pre><code>#include &lt;cstdio&gt; class A { public: virtual int potential()=0; virtual int potential(int arg, int arg2)=0; }; class B : public A { public: int potential() { return 1; } virtual int potential(int arg, int arg2) { return 2; } }; class C : public B { public: int potential(int arg, int arg2) { return 3; } }; int main(int argc, char** argv) { C c; int value = c.potential(); printf("Got %i\n", value); return 0; } </code></pre> <p>I have two pure virtual methods, both named <code>potential</code> in the abstract superclass <code>A</code>. The subclass <code>B</code> then defines both, but a further subclass <code>C</code> only needs to redefine one of the methods.</p> <p>However, on compilation, only the method defined in <code>C</code> is recognized, and <code>potential()</code> isn't seen (this should have been inherited from <code>B</code>):</p> <pre><code>In function 'int main(int, char**)': Line 23: error: no matching function for call to 'C::potential()' compilation terminated due to -Wfatal-errors. </code></pre> <p>If I rename <code>A::potential(int, int)</code> to something else all the way down the inheritance tree, such as <code>A::somethingElse(int, int)</code>, then the code compiles fine, and the output is <code>Got 1</code>, as expected.</p> <p>This has been verified using <strong>clang</strong>, <strong>g++</strong> and MSVC's <strong>cl</strong>.</p> <p>Any ideas on what is going on?</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.
 

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