Why does Reset(); work without class name?

I am a PHP developer with some Java background and I am wondering about the scope of various functions.

During the challenge of the constructor, I came up with

FBullCowGame::FBullCowGame() {
FBullCowGame::Reset();
}

In the video it was just Reset(); without “FBullCowGame::” in front of it. While I see that it indeed works just as fine, I am wondering why that is. In PHP/Java I would go for something like

this.Reset();

but never just type the function name.

The same of course applies to the getter functions, I thought, it should be something like

return this.MyMaxTries; //or
return FBullCowGame.MyMaxTries;

Would it be better style to include the class name or is it redundant because we are inside FBullCowGame.cpp?

The short answer - you’re right that it is redundant because we are inside the class scope.

Long answer - under certain circumstances you still may need to have the class name when you have inheritance (if you’re familiar with it) and want to call a similarly named parent class function (i.e. to resolve a naming conflict aka ambiguous function call). E.g. in Java you have the super keyword, but in C++ you’ll have to write the parent class name

super.Reset(); // Java

MyParentClass::Reset(); // C++

In our example, both this and FBullCowGame:: are implied by the compiler so we can skip them, but you also can have them like so:

FBullCowGame::FBullCowGame() {
    this->FBullCowGame::Reset(); // same as simply calling  Reset();
}
1 Like

Privacy & Terms