3

Consider the following example:

struct Base
{
    void foo()
    {
        cout << "Base foo\n";
    }

    void bar()
    {
        cout << "Base bar\n";
    }
};

struct Derivate : public Base
{
    void foo(int x)
    {
        cout << "Derivate foo\n";
    }
};

If we create two instances, like

Base a;
Derivate b;

The Base object a can call its member functions as usual (a.foo(); a.bar();).

When using b the call b.bar() works as expected but since I overloaded Base::foo() it's not possible to call b.foo().

Why is that?

Timo
  • 9,269
  • 2
  • 28
  • 58
  • Possible duplicate of https://stackoverflow.com/questions/1628768/why-does-an-overridden-function-in-the-derived-class-hide-other-overloads-of-the – Yksisarvinen Apr 17 '18 at 12:31

1 Answers1

-1

You didn't overload Base::foo, instead you've hid it. And it is still accessible, it can be called like this:

static_cast<Base &>(b).foo();
user7860670
  • 35,849
  • 4
  • 58
  • 84
  • 1
    I didn't downvote, but I'd guess whoever did, did so because you mix up "overload" and *override*. What is shown *does* overload. What requires a virtual function in the base is overridding. – underscore_d Apr 17 '18 at 12:31
  • 1
    `Base::foo` is indeed *overloaded* (and also *hidden*) by `Derivate::foo`. The fact that it's virtual prevents it from being *overridden*, not *overloaded*. And while the provided workaround does indeed work, a `using` declaration would be a much cleaner solution. – Angew is no longer proud of SO Apr 17 '18 at 12:31
  • @Angew It is not overloaded because it does not participate in overload resolution at all (unless `using Base::foo;` is used). – user7860670 Apr 17 '18 at 12:34
  • You're right that I misspoke about the overload bit. – Angew is no longer proud of SO Apr 17 '18 at 12:39