C++ 我应该在类中使用`this`吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9590820/
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
Should I use `this` within a class?
提问by Ben
Within a member function of a class in C++, does it make a difference, if I use this->dataMember
or just dataMember
? What is considered better style? Is there any performance difference?
在 C++ 中的类的成员函数中,如果我使用this->dataMember
或只是 使用它会有所不同dataMember
吗?什么被认为是更好的风格?有什么性能差异吗?
(I am not talking about the case where a local variable has the same name as the data member, in which case you must, to my knowledge, use this->
to distinguish between them.)
(我不是在谈论局部变量与数据成员同名的情况,在这种情况下,据我所知,您必须使用this->
来区分它们。)
回答by James Kanze
As a general rule, it's a question of local conventions. Most of the
places I've seen do not use this->
except when necessary, and that's
the convention I prefer as well, but I've heard of people who prefer to
use it systematically.
作为一般规则,这是当地惯例的问题。我见过的大多数地方this->
除了在必要时不使用,这也是我喜欢的惯例,但我听说有人喜欢系统地使用它。
There are two cases when it is necessary. The first is if you've hidden
the name with the same name in local scope; if e.g. you have a member
named toto
, and you also named your function argument toto
. Many
coding conventions mark either the member or argments to avoid this
case, e.g. all member names start with my
or m_
, or a parameter name
will start with the
.
有必要时有两种情况。第一个是如果您在本地范围内隐藏了具有相同名称的名称;例如,如果您有一个名为 的成员toto
,并且您还命名了函数参数toto
。许多编码约定标记成员或参数以避免这种情况,例如所有成员名称都以my
或开头m_
,或者参数名称将以 开头the
。
The other case is that this->
can be used in a template to make a name
dependent. This is relevant if a template class inherits from a
dependent type, and you want to access a member of the base, e.g.:
另一种情况是this->
可以在模板中使用以使名称相关。如果模板类从依赖类型继承,并且您想访问基类的成员,则这是相关的,例如:
template <typename T>
class Toto : public T
{
public:
int f()
{
return this->g();
}
};
Without the this->
here, g()
would be a non-dependent name, and the
compiler would look it up in the context of the template definition,
without taking the base class into consideration.
如果没有this->
here,g()
将是一个非依赖名称,编译器将在模板定义的上下文中查找它,而不考虑基类。
回答by Philipp
I always use this
when calling member functions.
我总是this
在调用成员函数时使用。
- It turns the function name into a dependent name so that base class member functions are found within a class template.
- It suppresses argument-dependent lookup. ADL has its advantages, but it can lead to surprising behavior, and I like it if it's not getting in my way.
- It has no real disadvantages, and so I use it for all member function calls for consistency reasons.
- I program in Python a lot where an explicit
self
is mandatory, so it's not a real burden for me.
- 它将函数名称转换为依赖名称,以便在类模板中找到基类成员函数。
- 它抑制了依赖于参数的查找。ADL 有其优势,但它会导致令人惊讶的行为,如果它不妨碍我,我喜欢它。
- 它没有真正的缺点,因此出于一致性原因,我将它用于所有成员函数调用。
- 我用 Python 编写了很多程序,其中显式
self
是强制性的,所以这对我来说并不是真正的负担。
But for data members I use it only when necessary because there is no ADL taking place. To answer your specific questions:
但是对于数据成员,我只在必要时使用它,因为没有发生 ADL。要回答您的具体问题:
Within a member function of a class in C++, does it make a difference, if I use this->dataMember or just dataMember?
在 C++ 中类的成员函数中,如果我使用 this->dataMember 或仅使用 dataMember,它会有所不同吗?
Yes, if this is within a class template. Then dataMember
is considered a non-dependent name, which can lead to semantic differences. For example:
是的,如果这是在类模板中。thendataMember
被认为是一个非依赖名称,这会导致语义差异。例如:
#include <iostream>
int i = 1;
struct R {
int i;
R(): i(2) { }
};
template<typename T>
struct S: T {
void f() {
std::cout << i << ' ' // selects ::i
<< this->i // selects R::i
<< std::endl;
}
};
int main() {
S<R>().f();
}
What is considered better style?
什么被认为是更好的风格?
I don't think that there is a strong opinion within the community about this. Use either style, but be consistent.
我不认为社区内对此有强烈的意见。使用任何一种风格,但要保持一致。
Is there any performance difference?
有什么性能差异吗?
I'm pretty sure there isn't.
我很确定没有。
回答by aschepler
This is a matter of style. Some people like the extra this->
to make it more obvious that you are accessing a class member. But if you feel it's obvious enough without it, there will be no difference in the generated code or performance.
这是一个风格问题。有些人喜欢额外的内容this->
,以便更明显地表明您正在访问班级成员。但是如果你觉得没有它就足够明显了,那么生成的代码或性能不会有什么不同。
(Besides the case you mentioned with overlapping scopes, this->
can also be mandatory in a template when trying to name a member of a type-dependent base class.)
(除了您提到的重叠范围的情况外,this->
在尝试命名依赖于类型的基类的成员时,也可以在模板中强制使用。)
回答by Not_a_Golfer
it's simply redundant to use this->
to call members, unless you want to semantically distinguish between locals and members quickly.
a lot of people use the m_
prefix for class members, to avoid writing this->
all the time.
使用它this->
来调用成员是多余的,除非您想在语义上快速区分本地人和成员。很多人使用m_
前缀作为类成员,以避免一直写this->
。
回答by Sebastian Flückiger
use this when you have a hidden/private member =) in any other case it does not make a difference =)
当您有隐藏/私人成员时使用它 =) 在任何其他情况下都没有区别 =)
from the IBM information center i quote the following
从 IBM 信息中心,我引用以下内容
Unless a class member name is hidden, using the class member name is equivalent to using the class member name with the this pointer and the class member access operator (->).
除非隐藏类成员名称,否则使用类成员名称等效于使用带有 this 指针和类成员访问运算符 (->) 的类成员名称。
回答by Pben
using "this->" is better (you are sure it's the members) but it's doesn't make a difference
使用“this->”更好(你确定它是成员)但它没有区别
回答by perreal
If a template function makes a call to a member function such that the call does not depend on any template parameters, this->
can be used to help the compiler as an alternative to MyUtopicClass<int, double, double>::vin()
.
如果模板函数调用成员函数使得该调用不依赖于任何模板参数,this->
则可用于帮助编译器作为MyUtopicClass<int, double, double>::vin()
.