Why does an overridden function in the derived class hide other overloads of the base class?

后端 未结 4 2161
一整个雨季
一整个雨季 2020-11-21 05:07

Consider the code :

#include 

class Base {
public: 
    virtual void gogo(int a){
        printf(\" Base :: gogo (int) \\n\");
    };

    v         


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-21 05:45

    Name hiding makes sense because it prevents ambiguities in name resolution.

    Consider this code:

    class Base
    {
    public:
        void func (float x) { ... }
    }
    
    class Derived: public Base
    {
    public:
        void func (double x) { ... }
    }
    
    Derived dobj;
    

    If Base::func(float) was not hidden by Derived::func(double) in Derived, we would call the base class function when calling dobj.func(0.f), even though a float can be promoted to a double.

    Reference: http://bastian.rieck.ru/blog/posts/2016/name_hiding_cxx/

提交回复
热议问题