Consider this demo program:
#include <stdio.h>
class Base
{
public:
virtual int f(int) =0;
virtual int f(){ return f(0); }
virtual ~Base(){ }
};
class Derived : public Base
{
public:
int f(int i)
{
return (10 + i);
}
};
int main(void)
{
Derived obj;
printf("%d\n", obj.f(1));
printf("%d\n", obj.f());
}
Which gives me the following compilation error:
virtualfunc.cpp: In function ‘int main()’:
virtualfunc.cpp:25:26: error: no matching function for call to ‘Derived::f()’
virtualfunc.cpp:15:9: note: candidate is: virtual int Derived::f(int)
My hope was that the call obj.f()would lead to the call Base::obj.f(), since the derived class did not define it, which would lead to the call Derived::obj.f(0)to be defined in the Base class.
What am I doing wrong here? Is there any way to do this? In particular, I would like the call to obj.f()return 10.
(Also note that I understand that I can use the default argument to solve this problem, but this code is just a brief example of my problem, so please don't tell me to use the default arguments.)
Thank.