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

后端 未结 4 2150
一整个雨季
一整个雨季 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:56

    The name resolution rules say that name lookup stops in the first scope in which a matching name is found. At that point, the overload resolution rules kick in to find the best match of available functions.

    In this case, gogo(int*) is found (alone) in the Derived class scope, and as there's no standard conversion from int to int*, the lookup fails.

    The solution is to bring the Base declarations in via a using declaration in the Derived class:

    using Base::gogo;
    

    ...would allow the name lookup rules to find all candidates and thus the overload resolution would proceed as you expected.

提交回复
热议问题