Proper use of this->

What is the proper use of the self-testing of this class?

I sometimes use it inside a method to clear that the variable used is a member variable and not one declared inside the method, but on the other hand I wonder if this is the reason for this, since I think you should always code (and comment ) in a way that is self-evident and therefore makes use of this unnecessary, and another reason against it would be that you are actually creating more code than necessary.

 void function() { while(i != this->length) //length is member var /*do something*/ } 

Another way to use it, I often meet inside the constructors (mainly in Java), since the parameters have the same name as the member variables that must be initialized. Since Primer claims this is bad code, I do not, but on the other hand I see how to use the same name as the -vars member as the names of the parameters from which they are used.

 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 that in fact there is a rule considering both languages ​​(Java / C ++), if I do not return only for C ++, since this is the one who interests me most.

+4
source share
6 answers

For the most part, this-> is redundant. The exception is if you have a parameter or a local variable with the same name (easily and probably best avoided), or in a template to force the next character to be dependent - with this last use it is often inevitable.

+10
source

this is not required here, since only members can be on initialization lists:

 Constructor::Constructor(int size,int value) : size(size), // no need for this-> value(value) {}; 

The only other use of this-> is in templates.

 template <class T> struct foo : T { void bar() { this->x = 5; // T has a member named x } }; 

If you do not need to use it, then no. Avoid having function parameters with the same name as members for objects other than constructors and set methods.

+2
source

using this-> explicitly indicates that you are referring to a method / member variable. Commenting out to say that this is just redundant ... if for some reason you have a local variable AND a member variable that has the same name and manages it differently in the same code block, for example :

 int x = 7; this->x = 8; // yes, I really mean the member variable here 
+1
source

Using this-> increases the length of your program, so it adds cognitive reading overhead. The only wise use of this is when there is something in scope that obscures the member of the class and you want to access this element. Shadow identifiers are a bad idea in most cases, and there is a compiler in gcc ( -Wshadow ) that helps to avoid this.

+1
source

Using this in a constructor is optional: compilers are smart enough to specify parameter names from member names. The snippet below compiles and works fine:

 Constructor::Constructor(int size,int value) : size(size), value(value) {}; 

In general, using this to disambiguate is acceptable, but unnecessary: ​​as you correctly noted, you can just use different names for parameters and members, and that is. However, there are times when you should use this , otherwise things are not going to compile. Here's a somewhat contrived example: using this to set up a backlink, follow these steps:

 class Callback { Client *client; public: Callback(Client* client) : client(client) {} // ... } class Client { Callback callback; public: Client() : callback(this) {} // You must use 'this' here // ... } 
+1
source

Personally, I prefer to name my variables in such a way that I can see that they are instance variables, for example. m_foo . I only use this-> if the compiler requires me because of some ambiguity, because I find the code becoming a little difficult to read with an expression that has a lot of this-> in them

0
source

Source: https://habr.com/ru/post/1383460/


All Articles