hjk41的日志

Avatar

Effective C++ Note 26: Guard against potential ambiguity

Potential ambiguity may occur in function overloading and multiple inheriting.

void f(char);
void f(int);

class Base1 {
public:
  int doIt();
};
class Base2 {
public:
  void doIt();
};
class Derived: public Base1,public Base2{
  ....
};


This code may work quite well for a long time, until one day, you call

float a=1.3;
f(a);    // ambiguous! call f(char) or f(int)? 

Derived d;
d.doIt();    // ambiguous! call Base1::doIt() or Base2::doIt()?


This means that you might release a library that can be called ambiguously without being aware that you are doing it.

In conclusion, do guard against potential ambiguity.

评论已关闭