Operator overloading

related topics
{math, number, function}
{work, book, publish}
{theory, work, human}

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 claimed to be useful because it allows the developer to program using notation "closer to the target domain"[1] and allows user-defined types a similar level of syntactic support as types built into the language. It can easily be emulated using function calls; for an example, consider the integers a, b, c:

a + b * c

In a language that supports operator overloading, and assuming the '*' operator has higher precedence than '+', this is effectively a more concise way of writing:

add (a, multiply (b,c))


[edit] Examples


 Time operator+(const Time& lhs, const Time& rhs) {
     Time temp = lhs;
     temp.seconds += rhs.seconds;
     if (temp.seconds >= 60) {
         temp.seconds -= 60;
     temp.minutes += rhs.minutes;
     if (temp.minutes >= 60) {
         temp.minutes -= 60;
     temp.hours += rhs.hours;
     return temp;

Addition is a binary operation, which means it has left and right operands. In C++, the arguments being passed are the operands, and the temp object is the returned value.


 Time Time::operator+(const Time& rhs) const {
     temp.seconds += rhs.seconds;
     if (temp.seconds >= 60) {
         temp.seconds -= 60;
     temp.minutes += rhs.minutes;
     if (temp.minutes >= 60) {
         temp.minutes -= 60;
     temp.hours += rhs.hours;
     return temp;


 bool Time::operator!() const {

[edit] Criticisms

Operator overloading has often been criticized because it allows programmers to give operators completely different semantics depending on the types of their operands. For example the use of the << in C++'s:

a << 1



Another, more subtle issue with operators is that certain rules from mathematics can be wrongly expected or unintentionally assumed. For example the commutativity of + (i.e. that a + b == b + a) does not always apply; an example of this occurs when the operands are strings, since + is commonly overloaded to perform a concatenation of strings (i.e. "school" + "bag" yields "schoolbag", which is different from "bag" + "school" yields "bagschool"). A typical counter to this argument comes directly from mathematics: While + is commutative on integers (and in general any real numbers), it is not commutative for other "types" of variable[citation needed]. It can be further noted that + is not even associative on floating point values in practice due to rounding errors. Another example: binary * (multiplication) is commutative for integers but not commutative in case of matrix multiplication.

[edit] 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
Limited set

[edit] Timeline of operator overloading

[edit] 1960s

The ALGOL 68 specification allowed operator overloading[6].

Full article ▸

related documents
Cotangent space
Column space
Monotone convergence theorem
Special linear group
Borel algebra
Automata theory
Golden ratio base
Least common multiple
Generating trigonometric tables
Associative algebra
Theory of computation
Product topology
Spectrum of a ring
Stirling's approximation
1 (number)
Sylow theorems
Measure (mathematics)
Jacobi symbol
Mean value theorem
De Morgan's laws
Free variables and bound variables
Diophantine equation
Cumulative distribution function
Cauchy-Riemann equations
Perfect number
Chain complex