Search code examples
c++classinheritancescopename-lookup

Overloaded child class function cannot call parent of similar name


I am assuming this is one of those "just not how it works" issues, but I fail to see why. Why do I need to qualify B's call to As Start with A::. If I change B::Start() to B::DoSomethingElse() I could call a parameter less Start() without A::. So what is happening?

#include <iostream>
#include <string>

class A {
  public:
    void Start(){
        
    }
};

class B : public A {
    public:
        void Start(int x){
            Start();     // cannot call this  
            A::Start();  // can call this  
        }
};

Solution

  • From the C++ standard (draft, emphasis mine) [basic.lookup.unqual]/1:

    In all the cases listed in 6.4.1, the scopes are searched for a declaration in the order listed in each of the respective categories; name lookup ends as soon as a declaration is found for the name. If no declaration is found, the program is ill-formed.

    So the Start name has already been found, within class B, so the lookup is halted. Overload resolution occurs only after name lookup is complete [basic.lookup]/1:

    ...Overload resolution (16.3) takes place after name lookup has succeeded....

    So even though class A and B have different parameters, this doesn't come into play here, since the name lookup is already complete.

    When you do A::Start(), you are then using qualified name lookup, where you are actually specifying the class where the function appears, so the name resolution will find that version.