1

I'm normally a C# guy, and it handles this fine, largely under its handling of "best match" resolving the method to call. I'm trying to do something similar in C++ now, but am getting a compile error. Long story short, it's a combination of method overloading and overriding.

class Bar : public Foo { } //Contents don't really matter here

class Base
{
public:
    virtual void Do(Foo* foo) { }
    virtual void Do(Bar* bar) { }
};
class Derived : public Base
{
public:
    virtual void Do(Bar* bar) { }
}


Foo* foo = new Foo();
Derived* d = new Derived();
d->Do(foo); //cannot initialize a parameter of type 'Bar*' with an rvalue of type 'Foo*'

So, it tries to resolve the method against only the methods in Derived rather than recognizing the base class implements a valid match. Again, C# finds the base method. Can C++ not do this, or am I missing something?

Adam Hewitt
  • 433
  • 6
  • 8

1 Answers1

4

A function in a derived class hides any functions of the same name in a base class (even if you're overriding a virtual function). To unhide the other overloads, use a "using" statement:

class Derived : public Base
{
public:
    virtual void Do(Bar* bar) { }
    using Base::Do;
};
aschepler
  • 70,891
  • 9
  • 107
  • 161