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();
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
c ++ method-signature virtual-inheritance method-overloading
Emme
source share