I'm having some trouble understanding in C++ the subtleties of argument-dependent lookup for member functions of class hierarchies. In the following code, the correct version of foo
is picked out by argument-dependent lookup if I call it through a base class reference. But if I call it from a derived class object, compilers (I tried several) reject the code, saying that the call to foo
is ambiguous. What is the fundamental difference between the two cases that is driving this behaviour ?
struct Base
{ virtual void foo(int) = 0;
virtual void foo(const char*) = 0;
};
struct Mid1 : virtual Base { void foo(int); };
struct Mid2 : virtual Base { void foo(const char*); };
struct Top : Mid1, Mid2 {};
int main()
{ Top t;
Base &b = t;
b.foo(1); //OK - Mid1.foo is called
t.foo(1); //error - ambiguous call to function
}