Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Operator overloading
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Criticisms== Operator overloading has often been criticized<ref>{{cite web |url=http://pages.cs.wisc.edu/~fischer/cs538.s08/lectures/Lecture08.4up.pdf |title=Issues in Overloading |last=Fisher |first=Charles N. |publisher=[[University of Wisconsin–Madison]] |year=2008}}</ref> because it allows programmers to reassign the semantics of operators depending on the types of their operands. For example, the use of the {{code|<<}} operator in [[C++]] <syntaxhighlight lang=Cpp inline>a << b</syntaxhighlight> shifts the bits in the variable {{code|a}} left by {{code|b}} bits if {{code|a}} and {{code|b}} are of an integer type, but if {{code|a}} is an output stream then the above code will attempt to write a {{code|b}} to the stream. 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 considered good practice to use operator overloading with care (the creators of [[Java (programming language)|Java]] decided not to use this feature,<ref>{{cite web |url=http://www.oracle.com/technetwork/java/simple-142616.html#4098 |website=The Java Language Environment |title=No more operator overloading |publisher=[[Oracle Corporation]]}}</ref> although not necessarily for this reason). Another, more subtle, issue with operators is that certain rules from mathematics can be wrongly expected or unintentionally assumed. For example, the [[Commutative property|commutativity]] of + (i.e. that {{code|1=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. {{code|"bird" + "song"}} yields {{code|"birdsong"}}, while {{code|"song" + "bird"}} yields {{code|"songbird"}}). A typical counter{{citation needed|date=September 2013}} to this argument comes directly from mathematics: While + is commutative on integers (and more generally any complex number), it is not commutative for other "types" of variables. In practice, + is not even always [[operator associativity|associative]], for example with floating-point values due to rounding errors. Another example: In mathematics, multiplication is commutative for real and complex numbers but not commutative in [[matrix multiplication]].
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)