I have a base class with a virtual fonction foo(int i)
and foo()
(same name, not same arguments).
The derived class override only foo()
.
Now the derived class doesn't knows foo(int i)
anymore.
class Base
{
public:
virtual void foo(int i) {
std::cout << "Base::foo(int i)" << std::endl;
}
virtual void foo() {
std::cout << "Base::foo()" << std::endl;
};
};
class Derived : public Base
{
public:
void foo() override {
std::cout << "Derived::foo()" << std::endl;
}
};
int main(const int ac, const char* const av[])
{
Base base;
base.foo();
base.foo(42);
Derived derived;
derived.foo();
derived.foo(42);//ERROR : Function unknown
}
Should output :
Base::foo()
Base::foo(int i)
Derived::foo()
Base::foo(int i)
Where does the problem come from and what would be the solutions?
The derived class will sometimes override foo()
, sometimes foo(int i)
and sometimes both.
Note
If I go through the base class it works, but this solution is not ideal in my program :
Derived derived;
Base* pBase = &derived;
pBase->foo();
pBase->foo(42); //WORK
Edit
Okay, I found on geeksforgeeks.org that you need to use using Base::foo;
in derivate class to import other foo functions.
Someone know why and what's going one in the backgourd? I don't like to not understand somethings.