1

I believe that the best answer is already given here : Why does an overridden function in the derived class hide other overloads of the base class?

But I am confused a little bit, specially with the statement :

In order to override this behavior, an explicit action is required from the user: originally a redeclaration of inherited method(s) (currently deprecated), now an explicit use of using-declaration.

Suppose I have the following program :

#include <iostream>
using namespace std;
class Base
{
public:
    int f(int i)
    {
        cout << "f(int): ";
        return i+3;
    }
};
class Derived : public Base
{
public:
    double f(double d)
    {
        cout << "f(double): ";
        return d+3.3;
    }
};
int main()
{
    Derived* dp = new Derived;
    cout << dp->f(3) << '\n';
    cout << dp->f(3.3) << '\n';
    delete dp;
    return 0;
}

I have two questions :

  1. Can I assume, w.r.t derived class object, the int f(int i) function does not exist at all. This is not inherited because of name hiding.

  2. If I have to use this function in Derived class, I have to define it again in derived class?

Community
  • 1
  • 1
Naveen
  • 7,944
  • 12
  • 78
  • 165
  • Following the comments in the link you posted, I came across this: http://www.stroustrup.com/bs_faq2.html#overloadderived Which is directly from stroustrup. – Tyler Jun 16 '16 at 14:04
  • `f(int)` *is* inherited - you can call it via `dp->Base::f(3)`, for example. It's just not found by unqualified name lookup. – Igor Tandetnik Jun 16 '16 at 14:04
  • 2
    The title text has nothing to do with the question - you're not asking *why* (which would be a bad question), you're asking specific things about name hiding. – Barry Jun 16 '16 at 14:06
  • i love how Stroutrup's 'answer' doesn't actually answer his own question "why doesn't it work" ? He just repeats the premise of the question: it doesn't work. no attempt to justify the 'why'. – Spongman May 24 '21 at 06:04

1 Answers1

4
  1. Can I assume, w.r.t derived class object, the int f(int i) function does not exist at all. This is not inherited because of name hiding.

It is inherited, it's just ... hidden, can't be found if you don't specify the scope (unqualified name lookup. You can specify it explicitly with scope resolution operator :: (qualified name lookup) :

dp->Base::f(3);
  1. If I have to use this function in Derived class, I have to define it again in derived class?

As the quoted answer said, you can do it with "an explicit use of using-declaration".

class Derived : public Base
{
public:
    using Base::f;
    ...
};

EDIT (for supplemental questions from comment)

  1. If it's name hidden, that means I can declare it again ? Same name, same parameters?

Yes, you can. It's still name hiding.

  1. If yes, what if I also added using Base::f along with the new declaration? Will it result in double definition?

No, it's not double definition. Using declaration just introduces the name into the derived class scope. And the member function declared in derived class will hide the one introduced from base class, it's still name hiding. (Note you still could call the one of base class by dp->Base::f(3);.)

If the derived class already has a member with the same name, parameter list, and qualifications, the derived class member hides or overrides (doesn't conflict with) the member that is introduced from the base class.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • If its name hidden, that means I can declare it again ? Same name, same parameters? If no, why? If yes, what if I also added `using Base::f` alongwith the new declaration? Will it result in double definition? – Naveen Jun 16 '16 at 14:12
  • I tried it, I can very well define the same function again in the derived class. Also, if I add `using Base::f`, I do not get the compiler error. But why? – Naveen Jun 16 '16 at 14:13
  • @InsaneCoder Yes you can define the same member function in derived class, and it will overrides (doesn't conflict with) the member that is introduced by using declaration from the base class. See [here](http://en.cppreference.com/w/cpp/language/using_declaration#In_class_definition). – songyuanyao Jun 16 '16 at 14:18