6

Today I felt like a noob:

class Base
{
public:
    virtual void foo(int)=0;
    virtual void foo(int, int) {}
    virtual void bar() {}
};

class Derived : public Base
{
public:
    virtual void foo(int) {}
};

void main()
{
    Derived d;
    d.bar(); // works
    d.foo(1); // works
    d.foo(1,2); // compiler error: no matching function call
}

I expected d to inherit foo(int, int) from Base, but it does not. So what am I missing here?

2 Answers 2

7

That's because the base functions with the same name are hidden.

You'll need to use using for the function you're not overriding:

class Derived : public Base
{
public:
    using Base::foo;
    virtual void foo(int) {}  //this hides all base methods called foo
};
Sign up to request clarification or add additional context in comments.

Comments

3

It's called Name hiding. You hide Base::foo(int, int) by providing Derived::foo(int)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.