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

StackOverflow https://stackoverflow.com/questions/23485241

Domanda

Say that six times fast... Why doesn't this compile in 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?
}
È stato utile?

Soluzione

When you override, you override the name, not the specific overload. So it hides all overloads from the base class. To address, you can put using A::foo; in your derived class to bring the overloads down into B.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top