I have code here that uses assignment operators that doesn't return by reference and it still works. So why does my book say you need to return by reference?
Here is a quote from my book:
The return type of operator= is a reference to the invoking object, so as to allow chained
assignments a=b=c.
The code below is from my book. I simply removed '&', in the original code that has assignment operators return by reference, from IntCell & operator=. This way the assignment operator no longer returns a reference, and it still works.
Returning by value will work, but you are making unnecessary copies every time the = operator is called, so it isn't preferable.
So what the author wrote was just misleading. I knew it would work even though it would be inefficient, but the author gave the impression that I had to use return by reference.
If you write (x = y) = z;, returning by reference and value would have different behavior.
For assigment operator it really does not matter, aside form inefficiency and principle of least astonishment: any person would expect some concrete behavior from something like: (x = y).set_field(5);