C++ 结合重载和覆盖导致奇怪的行为

标签 c++ overloading overriding

考虑以下代码

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/

相关文章:

c++ - dynamic_cast 派生模板到较少的 cv 限定指针

C++ 共同祖先问题。它是什么?

asp.net-mvc - 如何重载 MVC Controller 以避免重复公共(public)代码?

c++ - 这个函数调用在 C++ 中是如何模糊的?

java - Java 中的@Override 是什么?

c++ - 将 NativeAPI 与 Visual Studio 结合使用

c++ - 远程启动 C++ 应用程序

c++ - 使用 typedef 别名作为参数重载成员方法

java - 如何在其 toString() 函数的重写中修改枚举的字符串表示形式?

使用 get/set 表示法快速设置非原始成员