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;
}
1
  • Many thanks. My question is indeed a duplicate of that one, and responses to that are quite good. Commented Dec 13, 2013 at 17:20

2 Answers 2

4

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

class Derived : public Base {
public:
    using Base::Foo;
};
Sign up to request clarification or add additional context in comments.

Comments

2

You could define Derived::Foo() as:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.