Home > Operator overloading
In computer programming, operator overloading (less commonly known as ad-hoc polymorphism) is a specific case of polymorphism in which some or all of operators like +, = or == are treated as polymorphic functions and as such have different behaviours depending on the types of its arguments. Operators need not always be symbols. Operator overloading is usually only syntactic sugar. It can easily be emulated using function calls:
- a + b × c
In a language that supports operator overloading is effectively a more concise way of writing:
- operator_add (a, operator_multiply (b,c))
(Assuming the × operator has higher precedence than +.)
Operator overloading provides more than an aesthetic benefit when the language allows operators to be invoked implicitly in some circumstances. For example, this is the case with to_s operator in Ruby, which returns a string representation of an object.
1 Criticisms
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 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.
2 Catalog
Languages that support operator overloading and declaring new operators:
Languages that support operator overloading:
- Ada
- C++
- C#
- DThere have been several programming languages called D during the history of computing. The one discussed in this article is that created by Walter Bright. The one called Dialog Manager is discussed on another page. This language takes some features of C,
- PerlImage:Programming-republic-of-perl. gif|right|framed|Programming Republic of logo]] Perl also Practical Extraction and Report Language (a backronym, see below), is a programming language released by Larry Wall on December 18, 1987 that borrows features fr
- PicoPico is a programming language developed at the PROG lab at the dutch-speaking Free University of Brussels Vrije Universiteit Brussel VUB . The language was created to introduce the essentials of programming to non computer science students. Pico can be s (in a way)
- PythonPython is an interpreted, interactive programming language created by Guido van Rossum in 1990, originally as a scripting language for Amoeba OS capable of making system calls. Python is often compared to Tcl, Perl, Scheme, Java and Ruby. Python is develo
Languages that do not support operator overloading:
- CThe C Programming Language Brian Kernighan and Dennis Ritchie, the original edition that served for many years as an informal specification of the language The C programming language is a low-level standardized programming language developed in the early
- DelphiDelphi is a programming language and software development environment. It is produced by Borland (known for a time as Inprise). The Delphi language, formerly known as Object Pascal ( Pascal with object-oriented extensions) originally targeted only Microso
- PascalPascal is one of the landmark computer programming languages on which generations of students cut their teeth and variants of which are still widely used today. TeX and much of the original Macintosh operating system were written in Pascal. The Swiss comp
- Visual Basic
- Java (though it gives special meaning to + in some contexts)