Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>However, on compilation, only the method defined in C is recognized, and potential() isn't seen (this should have been inherited from B).</p> </blockquote> <p>C++ doesn’t work like this: because you implemented a different <code>potential</code> method (a method of the same name but with different parameters) in <code>C</code>, the other method is <em>hidden</em> as far as <code>C</code> is concerned.</p> <p>Hiding happens because of the way that C++ resolves (overloaded) method names: when you call a method <code>potential</code> on an instance of a class (here <code>c</code>), C++ searches in the class whether a method of that name exists. If that isn’t the case it continues its search in the base classes. It goes further up in the hierarchy until at least one method of that name is found.</p> <p>But in your case, C++ doesn’t have to search far: the method already exists in <code>C</code>, so it stops its search. Now C++ tries to match the method signature. Unfortunately, the method signature doesn’t match but at this time it’s too late: overload resolution fails; C++ doesn’t search for other methods that might match.</p> <p>There are three solutions:</p> <ol> <li><p>Import it with <code>using</code> in C:</p> <pre><code>class C : public B { public: using B::potential; int potential(int arg, int arg2) { return 3; } }; </code></pre></li> <li><p>Call the method from a base class instance in <code>main</code>:</p> <pre><code>C c; B&amp; b = c; int value = b.potential(); </code></pre></li> <li><p>Qualify the name explicitly in <code>main</code>:</p> <pre><code>C c; int value = c.B::potential(); </code></pre></li> </ol>
    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.
    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.
    3. 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