>>21
As you might notice from this example, it's entirely possible to overload operators with multiple operands. For example:
T & operator *(const T &);
T & operator *(int);
T & operator *(const U &);
This allows you to, for example, have a
Product object that expresses multiplication. Then you can use primitive integers on it as though it was normal multiplication, as well as being able to use other
Products. You wouldn't be able to achieve this effect any other way. You could also have an
Exponent object, which would represent U and allow you to modify the
Product accordingly.
This is what's meant by "exploit the intuitive properties of a class." The problem with this is that for any non-trivial function, it's rare that people agree on what the correct operator is. It's obvious enough for
string and maybe
vector, but after that, it gets a little hazy.
Actually, speaking of
vector, that's a good way of expressing the operator overloading. You might want to add an element to a vector or you might want to add entire vectors:
vector<T> & operator +(const T &);
vector<T> & operator +(const vector<T> &);
If you do use operator overloading, be
certain to document what the operators actually do, because what seems obvious to you might seem completely illogical to someone else: Like jumping off a building to learn how it feels to hit the ground.