C ++: inheriting an overloaded non-virtual method and a virtual method with the same name causes a problem

I am trying to inherit two equally named methods with different parameter lists into a derived class. One of them is virtual and redefined in a derived class, the other is not virtual. In doing so, I get a compilation error when I try to access a non-virtual method of the base class from an object of the derived class.

Here is a snippet of code

class Base { public: void f() { cout << "[Base::f()]" << endl; } virtual void f(int arg) { cout << "[Base::f(" << arg << ")]" << endl; } }; class Deriv : public Base { public: virtual void f(int arg) { cout << "[Deriv::f(" << arg << ")]" << endl; } }; int main() { Deriv d; df(-1); df(); // <<-- compile error return 0; } 

which produces the following compilation error:

error: there is no corresponding function to call in "Deriv :: f ()
note: candidates: virtual void Deriv :: f (int)

I am not an expert in C ++, but so far I have considered it correct to assume that member methods can be completely distinguished by their signatures. Thus, the non-virtual Base :: f () method should not be overridden and should remain available. Am I really wrong?

Here are some interesting / additional comments about this:

  • - the overriding method Deriv :: f (int arg) may also be non-virtual; an error occurs anyway
    - the error disappears / can be bypassed ...
    • ... by casting the Deriv object to the Base class
      ... when not overriding Base :: f (int arg) in Deriv
      ... adding the command "Base :: f;" to the public part of Deriv

So, since I already know how to avoid this compilation error, I mainly wonder why this error occurs! Please help me shed some light on this ...

Thanks at advanvce! emme

+6
c ++ method-signature virtual-inheritance method-overloading
source share
2 answers

In Deriv add this:

 using Base::f; 

In addition to the link provided by @DumbCoder, you can find more details in my answer to a similar question: Overriding an overloaded database in C ++

+7
source share

A derived class function hides the definition of a base function. A detailed explanation of why and how

+2
source share

All Articles