Bruce Eckel's Thinking in C++, 2nd Ed Contents | Prev | Next

Warning & reassurance

It’s very tempting to become overenthusiastic with operator overloading. It’s a fun toy, at first. But remember it’s only syntactic sugar, another way of calling a function. Looking at it this way, you have no reason to overload an operator except that it will make the code involving your class easier to write and especially read. (Remember, code is read much more than it is written.) If this isn’t the case, don’t bother.

Another common response to operator overloading is panic: Suddenly, C operators have no familiar meaning anymore. “Everything’s changed and all my C code will do different things !” This isn’t true. All the operators used in expressions that contain only built-in data types cannot be changed. You can never overload operators such that

1 << 4;

behaves differently, or

1.414 << 2;

has meaning. Only an expression containing a user-defined type can have an overloaded operator.

Contents | Prev | Next


Contact: webmaster@codeguru.com
CodeGuru - the website for developers.
[an error occurred while processing this directive]