何时应该在c++中使用“this”关键字?(复制)

时间:2022-09-10 23:44:29

Possible Duplicates:
Is excessive use of this in C++ a code smell

可能的重复:在c++中过度使用它是一种代码味道

Years ago, I got in the habit of using this-> when accessing member variables. I knew it wasn't strictly necessary, but I thought it was more clear.

几年前,我习惯在访问成员变量时使用这个->。我知道这不是绝对必要的,但我认为这更清楚。

Then, at some point, I started to prefer a more minimalistic style and stopped this practice...

然后,在某个时候,我开始喜欢一种更简约的风格,并停止了这种做法……

Recently I was asked by one of my more junior peers whether I thought it was a good idea and I found that I didn't really have a good answer for my preference... Is this really a wholly stylistic choice or are there real reasons why not prefixing this-> on member variable accesses is better?

最近,我的一个低年级同学问我是否认为这是个好主意,我发现我对自己的偏好并没有一个很好的答案……这真的是一个完全风格的选择吗?还是有真正的原因为什么不在成员变量访问上加上这个->更好?

7 个解决方案

#1


35  

While this is a totally subjective question, I think the general C++ community prefers not to have this->. Its cluttering, and entirely not needed.

虽然这是一个完全主观的问题,但我认为一般的c++社区不喜欢使用这个->。它杂乱不堪,完全不需要。

Some people use it to differentiate between member variables and parameters. A much more common practice is to just prefix your member variables with something, like a single underscore or an m, or m_, etc.

有些人用它来区分成员变量和参数。更常见的做法是在成员变量前面加上一些前缀,如单下划线、m或m_等。

That is much easier to read, in my opinion. If you need this-> to differentiate between variables, you're doing it wrong. Either change the parameter name (from x to newX) or have a member variable naming convention.

在我看来,这更容易读懂。如果你需要这个->来区分变量,你做错了。要么更改参数名称(从x改为newX),要么使用成员变量命名约定。

Consistency is preferred, so instead of forcing this-> on yourself for the few cases you need to differentiate (note in initializer lists this is completely well-defined: x(x), where the member x is initialized by the parameter x), just get better variable names.

一致性是首选的,因此,不要将这个->强加于您自己,因为您需要区分的少数情况(在初始化器列表中,这是完全定义好的:x(x),其中成员x由参数x初始化),只是得到更好的变量名。

This leaves the only time I use this: when I actually need the address of the instance, for whatever reason.

这是我唯一一次使用它:无论出于什么原因,当我实际需要实例的地址时。

#2


8  

Personally I never use this, except:

我个人从来不使用这个,除了:

  • when I need to pass 'this' as an argument to a method of another class
  • 当我需要将'this'作为参数传递给另一个类的方法时
  • in the implementation of the assignment operator
  • 在执行赋值操作符时

#3


7  

I can only recall doing it with

我只记得用过它

delete this;

#4


6  

When there is an ambiguity between, say, a function parameter and an instance variable.

当函数参数和实例变量之间存在模糊性时。

Of course such ambiguity should be avoided! It might be preferable to change the function parameter name instead of incurring overhead (i.e. prefixes) for all access to instance parameters though...

当然,这样的模棱两可应该避免!对于所有对实例参数的访问,最好更改函数参数名,而不是带来开销(例如前缀)。

#5


5  

It's usable when you have variables in a scope "above" the one you are working with.

当您在您正在处理的范围“上面”中有变量时,它是可用的。

int i;
public void foo() {
    int i;
    i = 3; // assign local variable
    this->i = 4; // assign global variable
}

Other than accessing variables in another scope, I myself agree with your "minimalistic choice". Less is more. :-)

除了在另一个范围中访问变量之外,我本人同意您的“极简选择”。少即是多。:-)

#6


3  

I like to use it for clarification, as when accessing members that were inherited. It reminds the reader where the variable came from if you don't have a naming convention that conveys that information.

我喜欢用它来澄清,比如在访问继承的成员时。它提醒读者变量来自哪里,如果你没有一个表示信息的命名约定。

You must use the this pointer when:

当:

  • Returning the current object.
  • 返回当前对象。
  • Setting up relations between objects (passing this into a constructor or setter)
  • 在对象之间建立关系(将其传递给构造函数或setter)
  • Checking for self reference: this != argPtr
  • 检查自我引用:this != argPtr

#7


2  

For me, it depends. If it is a short function or variable, I just type it in (e.g. mCount). Most of the time, however, I use very descriptive member variable and function names (e.g. mExclusivelyLockedDigitalIOList ). In those instances, I tend to use the this pointer to have Visual Studio's IntelliSense finish my typing for me. Saves on keystrokes and spelling mistakes.

对我来说,这取决于。如果是短函数或变量,我只需输入(例如mCount)。然而,大多数时候,我使用非常描述性的成员变量和函数名(例如mExclusivelyLockedDigitalIOList)。在这些情况下,我倾向于使用这个指针来让Visual Studio的IntelliSense替我完成我的输入。节省按键和拼写错误。

#1


35  

While this is a totally subjective question, I think the general C++ community prefers not to have this->. Its cluttering, and entirely not needed.

虽然这是一个完全主观的问题,但我认为一般的c++社区不喜欢使用这个->。它杂乱不堪,完全不需要。

Some people use it to differentiate between member variables and parameters. A much more common practice is to just prefix your member variables with something, like a single underscore or an m, or m_, etc.

有些人用它来区分成员变量和参数。更常见的做法是在成员变量前面加上一些前缀,如单下划线、m或m_等。

That is much easier to read, in my opinion. If you need this-> to differentiate between variables, you're doing it wrong. Either change the parameter name (from x to newX) or have a member variable naming convention.

在我看来,这更容易读懂。如果你需要这个->来区分变量,你做错了。要么更改参数名称(从x改为newX),要么使用成员变量命名约定。

Consistency is preferred, so instead of forcing this-> on yourself for the few cases you need to differentiate (note in initializer lists this is completely well-defined: x(x), where the member x is initialized by the parameter x), just get better variable names.

一致性是首选的,因此,不要将这个->强加于您自己,因为您需要区分的少数情况(在初始化器列表中,这是完全定义好的:x(x),其中成员x由参数x初始化),只是得到更好的变量名。

This leaves the only time I use this: when I actually need the address of the instance, for whatever reason.

这是我唯一一次使用它:无论出于什么原因,当我实际需要实例的地址时。

#2


8  

Personally I never use this, except:

我个人从来不使用这个,除了:

  • when I need to pass 'this' as an argument to a method of another class
  • 当我需要将'this'作为参数传递给另一个类的方法时
  • in the implementation of the assignment operator
  • 在执行赋值操作符时

#3


7  

I can only recall doing it with

我只记得用过它

delete this;

#4


6  

When there is an ambiguity between, say, a function parameter and an instance variable.

当函数参数和实例变量之间存在模糊性时。

Of course such ambiguity should be avoided! It might be preferable to change the function parameter name instead of incurring overhead (i.e. prefixes) for all access to instance parameters though...

当然,这样的模棱两可应该避免!对于所有对实例参数的访问,最好更改函数参数名,而不是带来开销(例如前缀)。

#5


5  

It's usable when you have variables in a scope "above" the one you are working with.

当您在您正在处理的范围“上面”中有变量时,它是可用的。

int i;
public void foo() {
    int i;
    i = 3; // assign local variable
    this->i = 4; // assign global variable
}

Other than accessing variables in another scope, I myself agree with your "minimalistic choice". Less is more. :-)

除了在另一个范围中访问变量之外,我本人同意您的“极简选择”。少即是多。:-)

#6


3  

I like to use it for clarification, as when accessing members that were inherited. It reminds the reader where the variable came from if you don't have a naming convention that conveys that information.

我喜欢用它来澄清,比如在访问继承的成员时。它提醒读者变量来自哪里,如果你没有一个表示信息的命名约定。

You must use the this pointer when:

当:

  • Returning the current object.
  • 返回当前对象。
  • Setting up relations between objects (passing this into a constructor or setter)
  • 在对象之间建立关系(将其传递给构造函数或setter)
  • Checking for self reference: this != argPtr
  • 检查自我引用:this != argPtr

#7


2  

For me, it depends. If it is a short function or variable, I just type it in (e.g. mCount). Most of the time, however, I use very descriptive member variable and function names (e.g. mExclusivelyLockedDigitalIOList ). In those instances, I tend to use the this pointer to have Visual Studio's IntelliSense finish my typing for me. Saves on keystrokes and spelling mistakes.

对我来说,这取决于。如果是短函数或变量,我只需输入(例如mCount)。然而,大多数时候,我使用非常描述性的成员变量和函数名(例如mExclusivelyLockedDigitalIOList)。在这些情况下,我倾向于使用这个指针来让Visual Studio的IntelliSense替我完成我的输入。节省按键和拼写错误。