Overloaded assignment operator
Posted on 2007-10-17
I am going through Deitel & Deitel's C++ book (section 8.8 of the fourth edition), in which they construct an Array class and show how to overload operators. The assignment operator is overloaded as follows:
const Array &operator=(const Array &);
According to D&D, the const return is designed to avoid (a1 = a2) = a3. My questions are:
1) Why is this necessary? After all, an assignment like (a1 = a2) = a3 works for ordinary variables.
2) What if you want to use this method on a non-constant Array object, or if you want it to return a non-constant Array? I can see it still works, but why don't the const declarations get in the way?