如果在 C++ 中使用它是否重要?二传手?
假设我有一个带有私有变量 x 的 C++ 类。对于它的setter来说,使用this
有什么区别吗?如果我不使用this
,是否可能会出现不需要/意外的行为?
Setter:
void setX(double input)
{
x = input;
}
使用 this
的 Setter:
void setX(double x)
{
this->x = x;
}
Suppose I have a c++ class with a private variable, x. For it's setter, is there any difference using this
? Is there the potential for unwanted / unexpected behavior is I don't use this
?
Setter:
void setX(double input)
{
x = input;
}
Setter using this
:
void setX(double x)
{
this->x = x;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这两个代码片段(假设它们是内联成员函数,因为没有
ClassName::
位)完全相同。使用您喜欢的任何一个。不过,我倾向于建议不要将参数命名为与成员变量相同的名称;很容易把它们搞混。These two code fragments (assuming they're inline member functions, since there's no
ClassName::
bit) are exactly equivalent. Use whichever you prefer. I would tend to recommend against naming parameters the same as member variables though; it's too easy get them mixed up.由于类模板的两阶段查找,可能需要
this
来(请参阅第一个示例) 两者是等效的。
在非模板情况下,您通常避免使用
this
;生成的代码将是相同的,因此不会有任何差异(参见第二个示例)。原因是编译器将尝试查找它看到的每个符号。第一个查找位置是类范围(块和函数范围除外)。如果它在那里找到该名称的符号,它将隐式发出
this
。实际上,成员函数只是带有不可见参数的普通函数。实际上是
由编译器转变成的。
模板中
this
的行为示例:输出:
非模板的示例程序集:
使用
this
:不使用
this
:验证自己:
test.cc:
运行 g++ -S test.cc。您将看到一个名为
test.s
的文件,您可以在其中搜索函数名称。Because of two-phase lookup for class templates,
this
might be required to(see the first example) Both are equivalent.
In a non-template situation, you usually avoid using
this
; the generated code will be the same, thus there won't be any difference (see the second example).The reason for this is that the compiler will try to lookup every symbol it sees. The first place to lookup is in class-scope (except for block and function scope). If it finds a symbol of that name there, it will emit
this
implicitly. In reality, member functions are just ordinary functions with an invisible parameter.Is actually transformed into
by the compiler.
Behavioral example for
this
in templates:Output:
Example assembly for non-template:
With
this
:Without
this
:Verify yourself:
test.cc:
Run
g++ -S test.cc
. You'll see a file namedtest.s
, there you can search for the function names.当涉及模板时,此变体会产生语法差异。然而,它在语义上是相同的;
this->
变体只是让您更清楚地知道您正在访问当前对象并避免潜在的冲突。The this variant makes a syntactical difference when templates are involved. However, it is semantically the same; the
this->
variant just makes it clearer you are accessing the current objects and avoids potential collisions.如果成员变量
x
会被参数x
遮盖,则明确需要this->
以确保您分配给正确的多变的。否则不需要。但我不建议您将参数命名为与成员变量相同的名称。您给出的第一个示例的陷阱较少。
If the member variable
x
would be obscured by the argumentx
, thenthis->
is explicitly needed to ensure you are assigning to the correct variable. Otherwise it is not needed.But I don't recommend you name your argument the same thing as a member variable anyway. The first example you give has fewer pitfalls.
它们之间没有区别。
There is no difference between them.
尽管您的代码可以工作,但它是糟糕的代码,并且可能会让某些人感到困惑。
最好给“setX()”参数一个不同的名称。
例如
Although your code will work, it is poor code and likely to confuse somebody.
Much better to give the "setX()" parameter a different name.
e.g.
没关系。我使用的代码约定规定所有私有变量都应通过 this 访问,以使代码更具可读性。但除了可读性之外,我认为没有任何区别。
Doesn't matter. I've worked with code conventions stating that all private vars should be accessed through this in order to make the code more readable. But other than readability I don't think there is any difference.