Overloaded child class function cannot call parent of similar name

Viewed 129

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  
        }
};
2 Answers

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.

The unqualified name Start used in the class definition of B

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

is searched at first in the scope of the class B and such a name is found because the class declares the function Start. That is the name Start declared in the derived class B hides the same name declared in the base class A. To access the name Start declared in the base class A you have to use the qualified name.

Related