[   ^ to index...   |   <-- previous   |   next -->   ]

Using overloaded operators

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);

Subtleties about overloading

Why operator overloading should be used sparingly

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?


Last modified: Tue Aug 1 00:58:48 PDT 2000