在C ++中重载+ = [英] Overloading += in c++
本文介绍了在C ++中重载+ =的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我已经重载了operator +和operator =,我是否仍然需要重载 运算符+ =可以使类似的东西起作用:
If I've overloaded operator+ and operator= do I still need to overload operator+= for something like this to work:
MyClass mc1, mc2;
mc1 += mc2;
推荐答案
operator + =不是+和=的组合,因此您确实需要显式重载它,因为编译器不知道为您创建难题.但仍然可以通过在operator + =中使用它们来从已经定义/重载的运算符中受益.
operator+= is not a composite of + and =, therefore you do need to overload it explicitly, since compiler do not know to build puzzles for you. but still you do able to benefit from already defined/overloaded operators, by using them inside operator+=.
这篇关于在C ++中重载+ =的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文