2

In the following code, "d.Foo()" throws a compiler error claiming that function Foo() does not take 0 arguments. Yet a 0-argument function with that name exists in the base class. The line "d.Base::Foo()" is acceptable.

I have a vague memory of learning that the use of a function name in a derived class hides all functions of that name in a base class, even though arguments may be different. I don't remember why, nor do I remember the best way to avoid that problem. Is my solution best, or is there another way to get at Base::Foo()?

Thanks very much!

RobR

// Override.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
class Base
{
public :
    void Foo()
    {
    }
};

class Derived : public Base
{
public: 
    void Foo(int x)
    {
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    Derived d;
    d.Foo();
    d.Base::Foo();

    return 0;
}
ROBERT RICHARDSON
  • 2,077
  • 4
  • 24
  • 57

2 Answers2

4

you can use(!) base class member functions via using

class Derived : public Base {
public:
    using Base::Foo;
};
stefan
  • 3,681
  • 15
  • 25
2

You could define Derived::Foo() as:

class Derived : public Base {
public:
    void Foo() { Base::Foo(); }
};
timrau
  • 22,578
  • 4
  • 51
  • 64