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


as read in books , in web, in c++ can overload "plus" or "minus" operators these prototypes (as member functions of class money):

const money operator +(const money& m2) const;

const money operator -(const money& m2) const;

and assignment operator with:

const money& operator =(const money& m2);

why use reference money object return value in assignment operator overloading , not in plus , minus operators?

returning reference assignment allows chaining:

a = b = c;  // shorter equivalent "b = c; = b;" 

(this work (in cases) if operator returned copy of new value, that's less efficient.)

we can't return reference arithmetic operations, since produce new value. (sensible) way return new value return value.

returning constant value, example does, prevents move semantics, don't that.


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -