OPERATOR OVERLOADING
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. Sometimes the overloadings are defined by the language, sometimes the programmer can implement support for new types.
Operator overloading 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:
add (a, multiply (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)
| Contents |
| Criticisms |
| Catalog |
| Timeline of operator overloading |
| 1960s |
| 1980s |
| Notes and references |
| See also |
Criticisms
Operator overloading has been criticized because it allows programmers to give operators completely different functionality depending on the types of their operands. C++'s usage of the
<< operator is an example of this problem. The expressiona << 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. Furthermore, even in the 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 some matrix operations are not commutative.Operators are overloaded so that the objects behave as primitive types. We cannot create new operators but only can modify the functionality of existing operators on our objects.
Catalog
A classification of some common programming languages by whether their operators are overloadable by the programmer and whether the operators are limited to a predefined set.
| Operators | Not overloadable | Overloadable |
|---|---|---|
| New definable | ★ ML ★ Pico [1] | ★ ★ Eiffel ★ Fortran [2] ★ F# ★ Haskell [3] ★ Lisp [4] ★ Prolog ★ Perl 6 ★ Smalltalk |
| Limited set | ★ C ★ Java ★ JavaScript ★ Objective-C ★ Pascal ★ BASIC ★ PHP | ★ Ada ★ C++ ★ C# ★ D ★ Object Pascal (Delphi, since 2005) ★ Perl ★ Python ★ Ruby ★ Visual Basic 2005 |
Notes:
1. binary functions with a symbolic name can be called infix
2. introduced in Fortran 90
3. type classes instead of overloading
4. operators use the same prefix syntax as functions
5. Report on the Algorithmic Language ALGOL 68, Section 10.2.2. A. van Wijngaarden, B.J. Mailloux, J.E.L. Peck and C.H.A. Koster, & al.
6. A History of C++: 1979−1991 - page 12 Bjarne Stroustrup
Timeline of operator overloading
1960s
The ALGOL 68 specification allowed operator overloading[5].
Extract from the 'ALGOL 68' language specification (page 177) where the overloaded operators ¬, =, ≠ and 'abs' are defined:
10.2.2. Operations on Boolean Operands
a) 'op' ∨ = ('bool' a, b) 'bool':( a | 'true' | b );
b) 'op' ∧ = ('bool' a, b) 'bool': ( a | b | 'false' );
c) 'op' ¬ = ('bool' a) 'bool': ( a | 'false' | 'true' );
d) 'op' = = ('bool' a, b) 'bool':( a∧b ) ∨ ( ¬b∧¬a );
e) 'op' ≠ = ('bool' a, b) 'bool': ¬(a=b);
f) 'op' 'abs' = ('bool' a)'int': ( a | 1 | 0 );
Note that no special declaration is required to ''overload'' an operator, and the programmer is free to create new operators.
1980s
C++'s operator overloading was further refined from that of ALGOL 68's. [6]
Notes and references
1. binary functions with a symbolic name can be called infix
2. introduced in Fortran 90
3. type classes instead of overloading
4. operators use the same prefix syntax as functions
5. Report on the Algorithmic Language ALGOL 68, Section 10.2.2. A. van Wijngaarden, B.J. Mailloux, J.E.L. Peck and C.H.A. Koster, & al.
6. A History of C++: 1979−1991 - page 12 Bjarne Stroustrup
See also
★ Polymorphism (computer science)
This article provided by Wikipedia. To edit the contents of this article, click here for original source.
psst.. try this: add to faves

العربية
中国
Français
Deutsch
Ελληνική
हिन्दी
Italiano
日本語
Português
Русский
Español