Once defined, user-overloaded operators can be used just like built-in C++ operators. Overload resolution proceeds identically to overload resolution of standard operators.
Vector v1, v2, v3; // ... some stuff, then: v1 = v2 + v3; // Matches Vector::operator+(const Vector& other) // Hence, effectively translated into // v1 = v2.operator+(v3);
Notice that we must return the resulting Vector by value, not by reference, because we expect a whole new Vector. This fits the semantics of the addition, which we expect to return a new value. (What are the alternatives if we did return by reference?)
Notice that the argument to operator+ is const, and that it is a const method. Why? Is this appropriate for all operators?
The tendency of programmers who first discover operator overloading is to define a whole slew of overloaded operators for every class they write. This is a bad idea. Each overloaded operator effectively extends the language that a programmer must know to understand code in a given library.
Also, in many cases it is not appropriate from an efficiency or design perspective to define certain operators. Consider, for example, the + operator we just defined for Vector. What methods/constructors/operators must be invoked for a simple statement such as the following?
v1 = v2 + v3; Also, consider: is this operator commutative? Do we expect it to be?