|
||||
|
Section 13:
|
[13.4] But operator overloading makes my class look ugly; isn't it supposed to make my code clearer?
Operator overloading makes life easier for the users of a class, not for the developer of the class! Consider the following example.
class Array {
public:
int& operator[] (unsigned i); // Some people don't like this syntax
...
};
inline
int& Array::operator[] (unsigned i) // Some people don't like this syntax
{
...
}
Some people don't like the keyword operator or the somewhat funny
syntax that goes with it in the body of the class itself. But the operator
overloading syntax isn't supposed to make life easier for the developer
of a class. It's supposed to make life easier for the users of the
class:
int main()
{
Array a;
a[3] = 4; // User code should be obvious and easy to understand...
...
}
Remember: in a reuse-oriented world, there will usually be many people who use
your class, but there is only one person who builds it (yourself); therefore
you should do things that favor the many rather than the few.
|
|||