Why we use reference return in assignment operator overloading and not at plus-minus ops?

前端 未结 5 589
予麋鹿
予麋鹿 2021-02-01 07:19

As I read in books and in the web, in C++ we can overload the \"plus\" or \"minus\" operators with these prototypes (as member functions of a class Money):

5条回答
  •  抹茶落季
    2021-02-01 07:38

    Because operator+ and operator- don't act on this object, but return a new object that is the summation (or subtraction) of this object from another.

    operator= is different because it's actually assigning something to this object.

    operator+= and operator-= would act on this object, and are a closer analog to operator=.

提交回复
热议问题