【问题标题】:Why isn't a non overridden overloaded function being inherited when I override one of its other overloads? [duplicate]当我覆盖其他重载之一时,为什么不继承未覆盖的重载函数? [复制]
【发布时间】:2014-05-06 02:54:51
【问题描述】:

说快六倍...为什么这在 MSVC 2010 中不能编译?

class A {
public:
    void foo(int a, int b) { };
    void foo(int a) { };
};
class B: public A {
public:
    void foo(int a, int b) { }; // <-- comment this out to compile
};

int main(int argc, char* argv[])
{
    B b;
    b.foo(1); // <-- doesn't compile... shouldn't B just inherit this overload?
}

【问题讨论】:

  • 你怎么这么快就找到了?我搜索了一会儿。不过我没有使用分辨率这个词。
  • 我用谷歌搜索了“继承重载函数”。
  • 感谢您的回答,这就是我所需要的。所以如果它是重复的,我该如何删除它?
  • 您可以删除自己的问题,但如果它有一个赞成的答案或超过 1 个答案,则不能。

标签: c++ visual-c++ inheritance overriding overloading


【解决方案1】:

当你覆盖时,你覆盖的是 name,而不是特定的重载。所以它隐藏了基类的所有重载。为了解决这个问题,您可以将using A::foo; 放入派生类中,以将重载降低到B

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    • 2011-11-11
    • 1970-01-01
    • 2019-01-04
    • 1970-01-01
    • 2015-09-19
    相关资源
    最近更新 更多