什么时候应该在 C++ 中使用“this”关键字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2337540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When should you use the "this" keyword in C++?
提问by dicroce
Possible Duplicates:
Is excessive use of this in C++ a code smell
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.
多年前,我养成了在访问成员变量时使用 this-> 的习惯。我知道这不是绝对必要的,但我认为它更清楚。
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?
最近,我的一个初级同龄人问我是否认为这是一个好主意,我发现我并没有真正为我的偏好找到好的答案......这真的是一个完全风格的选择还是有真正的原因为什么不在成员变量访问中添加前缀 this-> 更好?
采纳答案by GManNickG
While this is a totally subjective question, I think the general C++ community prefers notto have this->
. Its cluttering, and entirely not needed.
虽然这是一个完全主观的问题,我觉得一般C ++社区宁愿不有this->
。它杂乱无章,完全不需要。
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
, orm_
等。
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.
在我看来,这更容易阅读。如果你需要this->
区分变量,那你就错了。更改参数名称(从x
到newX
)或具有成员变量命名约定。
Consistency is preferred, so instead of forcing this->
on yourself for the fewcases 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.
一致性是首选,因此不要this->
在少数需要区分的情况下强迫自己(注意在初始化列表中这是完全明确定义的:x(x)
,其中成员x
由参数初始化x
),只需获得更好的变量名称。
This leaves the only time I use this
: when I actually need the address of the instance, for whatever reason.
这是我唯一使用的时间this
:当我实际上需要实例的地址时,无论出于何种原因。
回答by Tim
I can only recall doing it with
我只记得这样做
delete this;
回答by Patrick
Personally I never use this, except:
我个人从不使用它,除了:
- when I need to pass 'this' as an argument to a method of another class
- in the implementation of the assignment operator
- 当我需要将“this”作为参数传递给另一个类的方法时
- 在赋值运算符的实现中
回答by jldupont
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...
当然应该避免这种歧义!最好更改函数参数名称,而不是为所有对实例参数的访问而产生开销(即前缀),尽管...
回答by Patrick
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. :-)
除了访问另一个范围内的变量外,我本人同意您的“简约选择”。少即是多。:-)
回答by thebretness
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:
在以下情况下必须使用 this 指针:
- Returning the current object.
- Setting up relations between objects (passing
this
into a constructor or setter) - Checking for self reference:
this != argPtr
- 返回当前对象。
- 设置对象之间的关系(传递
this
给构造函数或 setter) - 检查自我参考:
this != argPtr
回答by bsruth
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 )。在这些情况下,我倾向于使用 this 指针让 Visual Studio 的 IntelliSense 为我完成输入。节省击键和拼写错误。