What is the proper use of the this
self-reference of a class?
I sometimes use it inside a method to clear out that the variable used is a member variable and not one declared inside the method, but on the other side I am wondering if this is a good reason to do so, as I think you should always code (and comment) in a way which is self-explaining and therefore would make such an unneeded use of this
unnecessary and another reason against it would be that you are actually producing more code than needed.
void function() {
while(i != this->length) //length is member var
/*do something*/
}
Another way of using it I frequently encounter is inside constructors (mostly in Java), as the parameters do have the same name as the member variables which has to be initialized. As the Primer states that this is bad code I am not doing this, but on the other side, I see how using the same name as the member-vars as parameter names clears out of which use they are.
C++
Constructor::Constructor(int size,int value) : this->size(size),
this->value(value) {};
Java
Constructor(int size,int value) {
this->size = size;
this->value = value;
}
I hope there is actually a rule considering both languages (Java/c++), if not I will retag for just c++ as this is the one I am more interested in.
Using
this->
increases the length of your program, so it adds to the cognitive overhead of reading it. The only sensible use of it is when there is something in scope that shadows a member of the class and you want to access the member instead. Shadowing identifiers is a bad idea in most cases and there is a compiler switch in gcc (-Wshadow
) that helps to avoid it.this
isn't necessary here as only members can be in init lists:The only other use of
this->
is in templates.If you don't have to use it then don't. Avoid having function parameters with the same name as members for things other than constructors and set methods.
Personally I prefer to name my variables in a way so that I see they are instance variables e.g.
m_foo
. I use onlythis->
if the compiler requires me to due to some ambiguity because i find the code gets a bit difficult to read with expression that have lots ofthis->
in themThe use of
this
in the constructor is not necessary: compilers are smart enough to tell parameter names from member names. The snippet below compiles and runs just fine:In general, use of
this
for disambiguation is valid, but unnecessary: as you correctly noted, you can simply use different names for parameters and members, and that's that. However, there are cases when you must usethis
, otherwise things are not going to compile. Here is a somewhat contrived example - usingthis
to set up a "back reference", as follows:For the most part,
this->
is redundant. The exception is if you have a parameter or a local variable of the same name (easily, and probably best avoided), or in a template, in order to force the following symbol to be dependent—in this latter use, it is often unavoidable.using
this->
explicitly states that you're referring to a member method/variable. Commenting to say this is just redundant... unless for some reason you have a local variable AND a member variable that have the same name and are manipulating them differently in the same code block, e.g.: