考虑以下代码
class X{
public:
virtual void foo(X x){ }
virtual void foo(int index){ }
};
class Y : public X{
public:
void foo(int index){ }
};
int main(){
Y y;
y.foo(X()); //Error, see below
}
类 X
重载了虚拟 foo
方法。一个版本采用 X
,另一个采用 int
。现在类 Y
继承自 X
并覆盖方法 foo(int)
。 foo(X)
方法不会被覆盖,它应该保持不变。
但是,当在 main
方法中创建类型为 Y
的对象并调用 foo(X)
时,编译器会报错如下:
In function ‘int main()’:
error: no matching function for call to ‘Y::foo(X)’
note: candidate is:
note: virtual void Y::foo(int)
note: no known conversion for argument 1 from ‘X’ to ‘int’
因此,唯一的候选者是重写的 foo(int)
方法。似乎另一种方法已经消失了。如果我删除覆盖版本,即将 Y
声明为 public Y : public X{};
,那么一切正常。为什么会这样?
最佳答案
当派生类定义一个与基类相同名称的成员时,派生类名称会隐藏基类名称。
在您的例子中,函数 Y::foo
隐藏 X::foo
。您需要将其纳入 Y
的范围:
class Y : public X{
public:
using X::foo; //it brings X::foo into the scope of Y
void foo(int index){ }
};
关于C++ 结合重载和覆盖导致奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12478287/