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

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist