C++——如何重载运算符+=?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4581961/
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
C++ -- How to overload operator+=?
提问by q0987
Given the following code snippet,
鉴于以下代码片段,
class Num
{
public:
Num(int iNumber = 0) : m_iNumber(iNumber) {}
Num operator+=(const Num& rhs)
{
this->m_iNumber = (this->m_iNumber + rhs.m_iNumber);
return *this;
}
private:
int m_iNumber;
};
//===========================================================
int _tmain(int argc, _TCHAR* argv[])
{
Num a(10);
Num b(100);
b += a;
return 0;
}
I would like to know how to correctly overload the operator+=
.
我想知道如何正确重载operator+=
.
Questions:
问题:
How to define the signature of this operator? Specially, what should be used for the return value?
How to implement the function body?
How to use this overload operator?
如何定义这个运算符的签名?特别是,返回值应该使用什么?
如何实现函数体?
如何使用这个重载运算符?
I have provided a solution as above but I have concerns that it is not correct.
我已经提供了上述解决方案,但我担心它不正确。
回答by Prasoon Saurav
Returning by referencewould be better
通过引用返回会更好
Num& operator+=(const Num& rhs){
this->m_iNumber += rhs.m_iNumber;
return *this;
}
回答by JUST MY correct OPINION
If you follow to this Wikibookyou'll find these answers by example:
如果您遵循此 Wikibook,您将通过示例找到这些答案:
Type& operator+=(const Type& right)
is the correct signature.- You did it right.
- You use an overloaded operator just like the plain operators, only with your type in place. That's kind of the point of the exercise. ;)
Type& operator+=(const Type& right)
是正确的签名。- 你做对了。
- 您可以像使用普通运算符一样使用重载运算符,但仅在类型就位的情况下。这就是练习的重点。;)
Note as an added point (which you did right) the compound assignment operators have to be member functions.
注意作为补充点(你做对了)复合赋值运算符必须是成员函数。
回答by templatetypedef
The signature of any of the assignment operators (operator= or operator @= for your favorite operator @) should be
任何赋值运算符(operator= 或 operator @= 代表您最喜欢的运算符 @)的签名应该是
Class& operator @= (const Class& rhs);
That is, the function takes its parameter by const reference (because it doesn't modify it), then returns a mutable reference to the object. The reason you return a non-const reference is because, for historical reasons, you can write code like this:
也就是说,该函数通过 const 引用获取其参数(因为它不会修改它),然后返回对该对象的可变引用。您返回非常量引用的原因是,由于历史原因,您可以编写如下代码:
(a += b) += c;
This is by no means good style, but it works for ints and so you should endeavor to make it work for your types as well.
这绝不是好的风格,但它适用于整数,因此您应该努力使其也适用于您的类型。
As for the body of your function, what you have there is perfectly correct. In general, though, you should be sure that the code works even if the parameter is the receiver object. For example, if you write something like
至于你的函数体,你有什么是完全正确的。但是,一般而言,即使参数是接收器对象,您也应该确保代码有效。例如,如果你写这样的东西
a += a;
This gets translated into
这被翻译成
a.operator+= (a);
And so the code will operate with both the parameter and the receiver being the same object. This usually doesn't come up for compound assignment operators (usually only operator=
needs to worry about this), but in some cases you can get burned if you're not careful.
因此,代码将在参数和接收器都是同一个对象的情况下进行操作。这对于复合赋值运算符通常不会出现(通常只operator=
需要担心这一点),但在某些情况下,如果您不小心,您可能会被烧毁。
Finally, you can use this operator +=
function just as you have been in the example code above. Any use of +=
automatically calls it.
最后,您可以operator +=
像在上面的示例代码中一样使用此函数。任何使用+=
自动调用它。
Hope this helps!
希望这可以帮助!
回答by Lightness Races in Orbit
回答by Billy ONeal
Your example is completely correct: http://codepad.org/PVhQw9sc.
你的例子是完全正确的:http: //codepad.org/PVhQw9sc。
- You can define the return value to be whatever you want. If you wanted it to match what
int
does, it would need to return the typeNum&
, and return the value*this
. - You did it correctly.
- You also did that correctly.
- 您可以将返回值定义为您想要的任何值。如果你想让它匹配什么
int
,它需要返回 typeNum&
,并返回 value*this
。 - 你做对了。
- 你也做对了。
回答by CnCPPcoder
Your operator function could also be written as
您的运算符函数也可以写成
Num& operator += (const Num& rhs)
{
m_iNumber += rhs.m_iNumber;
return m_iNumber;
}
*this->m_iNumber
and m_iNumber
by itself within a member function are the same and using the former format is more typing in my opinion.
*this->m_iNumber
并且m_iNumber
在成员函数中本身是相同的,在我看来,使用前一种格式更容易打字。