Operator overloading |
In computer programming, operator overloading (less commonly known as operator ad-hoc type polymorphism) is a specific case of polymorphism (computer science) in which some or all of operator (programming)s 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. 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.)
= Criticisms =
Operator overloading has been criticised because it allows programmers to give operators completely different functionality depending on the types of their operands. C plus plus s usage of the << 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 programmer to change the usual semantics of an operator, 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 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.
Another, more subtle issue with operators is that certain rules from mathematics can be expected or unintentionally assumed. For example, a + b usually means the same as b + a, but school + bag is different from bag + school in languages that overload + for string concatenation.
= Catalog =
A classification of some programming languages by whether their operators are overloadable by the programmer and whether the operators are limited to a predefined set. Operators No overloading Not overloadable Overloadable Limited set
*C programming language *Java programming language *Objective-C *Pascal programming language *Visual Basic programming language *PHP
*Ada programming language *C plus plus *C sharp *D programming language *Delphi programming language *Perl *Python programming language *Lisp_programming_language New definable
*ML programming language
*Pico programming language
*ALGOL 68#Operators *Fortran *Haskell programming language *PostgreSQL *Prolog *Ruby programming language *Smalltalk
Notes: # operators use the same Prefix notation syntax as functions # binary functions with a symbolic name can be called infix # introduced in Fortran 90 # type classes instead of overloading|
|