[英]C++ when to use an & in front of an operator overloading function
When do you need to use an &
before the operator declaration? 什么时候需要在操作员声明之前使用
&
?
Example: 例:
class ClassName {
public:
// Some constructor here..
ClassName operator+(...)
ClassName operator*(...)
ClassName &operator+=(...) < has an &
ClassName operator==(...)
ClassName &operator<<(...) < has an &
private:
// Some member variables here..
}
When you want to distinguish a postfix and prefix i++
or ++i
you use an &
当你想区分后缀和前缀
i++
或++i
你使用&
ClassName &operator++()
ClassName operator++(int)
But when do you use an &
for the other operator overload functions? 但是什么时候使用
&
作为其他运算符的重载函数? Is there some kind of rule or has it something to do with the memory? 是否有某种规则或与记忆有关?
tl;dr: Same as with any function. tl; dr:与任何函数相同。 Do you return by value, or by reference?
您是按价值还是按参考价格退货?
It might be clearer if you align your ampersand to the left (which has no semantic effect, just like with char* str
vs char *str
): 如果你将你的&符号左对齐(它没有语义效果,就像
char* str
vs char *str
)可能会更清楚:
ClassName& operator++()
ClassName operator++(int)
So the choice depends on whether you want to return a reference to the existing object: 因此,选择取决于您是否要返回对现有对象的引用:
ClassName& operator++()
{
this->someMember += 1;
return *this;
}
…or a nice new one: ......还是一个不错的新人:
ClassName operator++(int)
{
// Post-fix operator so we have to return the "old" unmodified version
ClassName old = *this;
this->someMember += 1;
return old;
}
In the first case, returning by value would be confusing to the user of your ++
, because: 在第一种情况下,按值返回会使
++
的用户感到困惑,因为:
++
would not affect the original object. ++
结果的进一步操作不会影响原始对象。 In the second case, returning by reference would be bad because: 在第二种情况下,通过引用返回会很糟糕,因为:
When do you need to use an & before the operator declaration?
什么时候需要在操作员声明之前使用&?
The & symbol declares the return type of the function to be a reference. &符号声明函数的返回类型作为引用。 More specifically, an lvalue reference.
更具体地说,左值参考。 So, you use & when you want to return an lvalue reference, and don't use & when you want to return a non-reference.
因此,当您想要返回左值引用时,使用&,当您想要返回非引用时,请不要使用&。
So, when do you want to return a reference from an operator overload? 那么,您何时想要从运算符重载返回引用? A concise rule of thumb is that you should return a reference if the built-in operator for non class types is an lvalue expression, and return a non-reference when the built-in operator is an rvalue expression.
一个简明的经验法则是,如果非类型类型的内置运算符是左值表达式,则应返回引用;如果内置运算符是右值表达式,则返回非引用。 There are exceptions to this.
这有例外。 For example, sometimes you cannot return a reference.
例如,有时您无法返回引用。 Perhaps you need to return some sort of wrapper object that behaves like a reference;
也许你需要返回一些行为类似于引用的包装器对象; such wrappers are typically returned by value.
这些包装器通常按值返回。
Assignment operators, including the compound assignment operators such as +=
conventionally return a reference to *this
. 赋值运算符,包括复合赋值运算符,例如
+=
通常会返回对*this
的引用。
Postfix operator conventionally returns the previous value. Postfix运算符通常返回先前的值。 As such, it cannot return a reference to
*this
, which contains the current value. 因此,它不能返回对
*this
的引用,它包含当前值。 Prefix operator does return the current value, so it can return a reference. 前缀运算符确实返回当前值,因此它可以返回引用。 The prefix increment of a non-class object is an lvalue expression, so returning an lvalue (ie a reference) from the operator overload is a good convention.
非类对象的前缀增量是左值表达式,因此从运算符重载返回左值(即引用)是一个很好的约定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.