In the example you've posted, there is no difference between the two syntaxes. They're completely identical.
There are two cases where there is a difference. One is if you have a template class that inherits from a type that depends on a template argument. For example:
template <typename T> class Base {
public:
void doSomething() const {
std::cout << "Do ALL the things!" << std::endl;
}
};
template <typename T> class Derived: public Base<T> {
public:
void doSomethingElse() const {
doSomething(); // Error!
this->doSomething(); // Okay
}
};
Here, since Derived<T>
inherits from the type Base<T>
, which depends on a template argument, name lookup is done in a two-step process. If you call doSomething
using the unqualified syntax, then the compiler doesn't know to look into Base<T>
to find it and will report an error. However, if you say this->doSomething()
, it knows that you're calling a member function and will eventually figure out that it's supposed to look in Base<T>
.
The other case arises if you have a local object declared inside the function whose name is the same as a member function. For example:
class ThisIsSillyDontDoThisLikeSeriouslyDont {
public:
void doSomething() const {
std::cout << "Do ALL the things!" << std::endl;
}
void weirdFunction() const {
auto doSomething = [] {
std::cout << "I'm afraid there's nothing to be done" << std::endl;
};
doSomething(); // Calls the local function
this->doSomething(); // Calls the member function
}
};
This second case is so rare that I've never seen it, and I'd go so far as to say that it's really poor coding style.
Aside from these rare cases, though, there's no difference between calling a member function on yourself with and without this->
as a prefix.