为什么当我覆盖未覆盖的重载函数的其他重载之一时,没有继承该函数?

Why isn't a non overridden overloaded function being inherited when I override one of its other overloads?

本文关键字:重载 函数 覆盖 一时 继承 其他 为什么      更新时间:2023-10-16

快说六遍…为什么这不能在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?
}

当您重写时,您重写的是名称,而不是特定的重载。因此它隐藏了基类的所有重载。为了解决这个问题,您可以将using A::foo;放在派生类中,从而将重载放到B中。