devxlogo

Resolving Lookup Ambiguity in Multiply-Inherited Classes

Resolving Lookup Ambiguity in Multiply-Inherited Classes

When multiple base classes have members with identical names, this can lead to ambiguities in the derived class, even if the signatures of the member functions in question are distinct. For example:

 struct  A{ void f();};struct B{ void f(void*);};class C: public A, public B{};int main(){ C c; c.f(&c); // compilation error: 'f' is ambiguous}

The problem has to do with the lookup rules of C++. The lookup process stops at the first scope where the name is found, i.e., the derived class, without performing a complete overloading resolution. Note that if we declared the two member functions inside class C, the compiler would resolve the call properly.
Fortunately, there is a solution: inject the functions

devx-admin

Share the Post: