以下代码:
class Base1
{
public:
void go() {}
};
class Base2
{
public:
void go(int a) {}
};
class Derived : public Base1, public Base2 {};
int main()
{
Derived d;
d.go(3);
return 0;
}
在编译期间会出错:
g++ -o a a.cc
a.cc: In function ‘int main()’:
a.cc:19:7: error: request for member ‘go’ is ambiguous
a.cc:10:10: error: candidates are: void Base2::go(int)
a.cc:4:10: error: void Base1::go()
make: *** [a] Error 1
很容易看出基类中的原型是不同的。但是为什么编译器无法检测到这一点并自动选择匹配的呢?
答案 0 :(得分:4)
跨类边界不允许函数重载。 您可以通过编写Derived类来解决此问题 -
class Derived : public Base1, public Base2
{
public:
using Base1::go;
using Base2::go;
};