Encyclopedia > Operator overloading

  Article Content

Operator overloading

Operator overloading (less commonly known as ad-hoc polymorphism) is a programming technique in which some or all of operators like +, = or == can be treated as functions and can be defined or redefined for new types. Operators need not be symbols. Sometimes defining new operators is allowed. This is usually only a syntactic sugar, and it can be easily emulated by function calls:

with operator overloadinga + b * c
without operator overloadingour_new_type_add (a, our_new_type_multiply (b,c))

Only in case when operators can be called implicitly they are of some use other than aesthetics. This is the case with Ruby operator to_s, which returns a string representation of an object and with operators in PostgreSQL, where mathematical transformations can be defined on operators and PostgreSQL may use many optimalizations to expressions that use them.

Operator overloading has been criticised because it allows programmers to give operators completely different functionality depending on the types of their operands. C++'s usage of the "<<" operator is a particularly bad example: The expression

 a << 1
will return two times the value of a if a is an integer variable, but if a is an output stream instead this will write "1" into it.

Please add more languages to this list

Languages that support operator overloading and declaring new operators: PostgreSQL version of SQL, Ruby, Haskell

Languages that support operator overloading: Ada, C++, C#, D, Delphi , Python

Languages that don't support operator overloading: C, Pascal, Java, Visual Basic

All Wikipedia text is available under the terms of the GNU Free Documentation License

  Search Encyclopedia

Search over one million articles, find something about almost anything!
  Featured Article
Jamesport, New York

... 0.26% from other races, and 0.46% from two or more races. 6.36% of the population are Hispanic or Latino of any race. There are 605 households out of which 26.1% have ...

This page was created in 38.8 ms