2

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  
        }
};
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • What's happening is that simply this is how C++ works. There are things you can do to adjust the symbol lookups in these cases, via the `using` statement. See your C++ textbook for more information. – Sam Varshavchik Jul 21 '20 at 13:34

2 Answers2

6

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.

ChrisMM
  • 8,448
  • 13
  • 29
  • 48
2

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335