Tips for Overloading Arithmetic Operators

Overloading the + operator does not mean that the += operator is overloaded. You must overload that operator separately. If you do, make sure that the normal identity relationships are maintained, that is, a += b has the same effect as a = a + b.

If you're overloading operators for a class whose objects are relatively large, you should pass parameters as references rather than by value. You should also be sure to pass references to constants, which allows constant objects to be operands.

The return type of an overloaded operator depends on the specific operator. Overloaded + or * operators for the Fraction class must return Fraction objects. Operators like += and *=, on the other hand, can return references to Fraction objects for efficiency. This is because + and * create temporary objects containing new values, and they cannot return references to objects created within the function. In contrast, += and *= modify an existing object, *this, so they can safely return references to that object. (Recall that the overloaded = operator, described in Chapter 5, also returns a reference to an object.)