In computer programming, operator overloading (less commonly known as operator ad-hoc polymorphism) is a specific case of polymorphism in which some or all of operators like +, = or == have different implementations depending on the types of their arguments. Operators need not always be symbols. Sometimes the overloadings are defined by the language, sometimes the programmer can implement support for new types.
Operator overloading (operator syntax and overloading generally) is usually only syntactic sugar. Despite this it is useful because it allows the developer to program using notation closer to the target domain and allows user types to look like types built into the language. It can easily be emulated using function calls; for an example, consider for integers a, b, c:
a + b × c
In a language that supports operator overloading this is effectively a more concise way of writing:
operator_add_integers (a, operator_multiply_integers (b,c))
(Assuming the × operator has higher precedence than +.)
However, in C++ templates or even C macros, operator overloading is needed in writing down generic, primitive operations such as summing elements when implementing a vector template. The only way to write primitive addition is a + b and this works for all types of elements only because of its overloading. (Actually, in C++ one could define an overloaded function add to use instead, but not in C.)
A more general variant of operator overloading, called expression reduction, allows expressions containing one or multiple operators to be reduced to a single function call. The expression above could be reduced to:
operator_add_and_multiply_integers(a, b, c)
<< operator is an example of this problem. The expression
a << 1
will return twice the value of a if a is an integer variable, but if a is an output stream instead this will write "1" to it. Because operator overloading allows the original programmer to change the usual semantics of an operator and to catch any subsequent programmers by surprise, it is usually considered good practice to use operator overloading with care.
The common reply to this criticism, given by programmers who favor operator overloading, is that the same argument applies to function overloading as well. Further, even in absence of overloading, a programmer can define a function to do something totally different from what would be expected from its name. An issue that remains is that languages such as C++ provide a limited set of operator symbols, thus removing from programmers the option of choosing a more suitable operator symbol for their new operation.
Another, more subtle issue with operators is that certain rules from mathematics can be expected or unintentionally assumed. For example, a + b usually (but not always) means the same as b + a, but "school" + "bag" is different from "bag" + "school" in languages that overload + for string concatenation. The same kind of difference exists in the world of mathematics, where matrix operations are not commutative.
| Operators | Not overloadable | Overloadable |
|---|---|---|
| Limited set | ||
| New definable |
Notes:
Überladen | Surcharge des opérateurs | Operator-overloading | Przeciążanie operatorów | Перегрузка операторов | 运算符重载
This article is licensed under the GNU Free Documentation License.
It uses material from the
"Operator overloading".
Home Page • arts • business • computers • games • health • hospitals • home • kids & teens • news • physicians • recreation• reference • regional • science • shopping • society • sports • world