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

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -