Consider this example of two similar C++ member functions in a class C:
void C::function(Foo new_f) {
f = new_f;
}
and
void C::function(Foo new_f) {
this->f = new_f;
}
Are these functions compiled in the same manner? Are there any performance penalties for using this-> (more memory accesses or whatever)?
Yes, it’s exactly the same and you’ll get the same performance.
The only time you really must use the
this->syntax is when you have an argument to the function with the same name as an instance variable you want to access. Using the name of the variable by itself will refer to the argument so you need thethis->. Of course, you could just rename the argument too. And, as ildjarn has pointed out in the comments also, you need to usethisin certain situations to call functions that are dependent becausethisis implicitly dependent (you can read more about that though).