Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: Intermediate
Apr 12, 2002

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
Danny Kalev
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date