Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's going on with overriding and overloading here in C++?

This doesn't work:

class Foo
{
public:
    virtual int A(int);
    virtual int A(int,int);
};
class Bar : public Foo
{
public:
    virtual int A(int);
};

Bar b;
int main()
{
    b.A(0,0);
}

It seems that by overriding Foo::A(int) with Bar::A(int) I have somehow hidden Foo::A(int,int). If I add a Bar::A(int,int) things work.

Does anyone have a link to a good description of what's going on here?

like image 227
BCS Avatar asked Jan 24 '23 04:01

BCS


1 Answers

Essentially, name lookup happens before overload resolution so the function A in your derived class overrides the virtual function in the base class but hides all other functions with the same name in any base classes.

Possible solutions include adding a using Foo::A; directive into your derived class to make all the base class members called A visible in the derived class or using different names for functions with different signatures.

See here as well.

like image 52
CB Bailey Avatar answered Feb 04 '23 18:02

CB Bailey