Proper use of this->

2019-06-24 05:38发布

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.

标签: java c++ this
6条回答
\"骚年 ilove
2楼-- · 2019-06-24 05:40

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.

查看更多
狗以群分
3楼-- · 2019-06-24 05:42

this isn't necessary here as only members can be in init 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 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.

查看更多
Evening l夕情丶
4楼-- · 2019-06-24 05:42

Personally I prefer to name my variables in a way so that I see they are instance variables e.g. m_foo. I use only this-> 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 of this-> in them

查看更多
霸刀☆藐视天下
5楼-- · 2019-06-24 05:44

The 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:

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

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 use this, otherwise things are not going to compile. Here is a somewhat contrived example - using this to set up a "back reference", as follows:

class Callback {
    Client *client;
public:
    Callback(Client* client) : client(client) {}
    // ...
}

class Client {
    Callback callback;
public:
    Client() : callback(this) {} // You must use 'this' here
    // ...
}
查看更多
爷的心禁止访问
6楼-- · 2019-06-24 05:51

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.

查看更多
萌系小妹纸
7楼-- · 2019-06-24 06:05

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.:

int x = 7;
this->x = 8; // yes, I really mean the member variable here
查看更多
登录 后发表回答