I recently encountered a difference between the two ways of calling a parent method. You can either use this->method() or not. The two are apparently not semantically equivalent, at least when it comes to template classes. Is there a synopsis somewhere online about the differences between the two? Thanks.
To my knowledge, explicitly putting this-> before a function call or member var is redundant and has no impact on the resulting code. The only time it would matter is if it gets around a scope / name conflict issue:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
class Example
{
int var;
int foo;
void func(int var)
{
var = 5; // modifies local 'var'
this->var = 5; // modifies Example::var
foo = 5; // modifies Example::foo
this->foo = 5; // also modifies Example::foo
}
};
+1 Disch
What do you mean by parent method? There is special syntax for calling the implementation of a method in the base class. It is BaseClassName::method(..). But you can also say this->BaseClassName::method(..). Templates are not relevant at all (I think).
The candidate names of member variables, getter/setters, arguments in getter/setter and constructors are predisposed to be identical. So you resolve them with appending underscores, prepending prefixes to the names and all sorts of nuisances. Using "this->" is one less inventive solution with similar effect. ne555 will tell you that getters and setters are evil, but that's a different story.
I don't recall exactly when, but there are some cases which accessing templates members gives some issues ( at least with GCC ) which can be solved by explicitly prepending this-> to the member