Today I felt like a noob:
class Base
{
public:
virtual void foo(int)=0;
virtual void foo(int, int) {}
virtual void bar() {}
};
class Derived : public Base
{
public:
virtual void foo(int) {}
};
void main()
{
Derived d;
d.bar(); // works
d.foo(1); // works
d.foo(1,2); // compiler error: no matching function call
}
I expected d
to inherit foo(int, int)
from Base
, but it does not. So what am I missing here?
That's because the base functions with the same name are hidden.
You'll need to use using
for the function you're not overriding:
class Derived : public Base
{
public:
using Base::foo;
virtual void foo(int) {} //this hides all base methods called foo
};
It's called Name hiding
. You hide Base::foo(int, int)
by providing Derived::foo(int)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With