I have a function with the same name, but with different signature in a base and derived classes. When I am trying to use the base class's function in another class that inherits from the derived, I receive an error. See the following code:
class A
{
public:
void foo(string s){};
};
class B : public A
{
public:
int foo(int i){};
};
class C : public B
{
public:
void bar()
{
string s;
foo(s);
}
};
I receive the following error from the gcc compiler:
In member function `void C::bar()': no matching function for call to `C::foo(std::string&)' candidates are: int B::foo(int)
If I remove int foo(int i){};
from class B
, or if I rename it from foo1
, everything works fine.
What's the problem with this?
Best Answer
It is because name lookup stops if it finds a name in one of your bases. It won't look beyond in other bases. The function in B shadows the function in A. You have to re-declare the function of A in the scope of B, so that both functions are visible from within B and C:
Edit: The real description the Standard gives is (from 10.2/2):
It has the following to say in another place (just above it):
([...] put by me). Note that means that even if your foo in B is private, the foo in A will still not be found (because access control happens later).