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
Ad hoc polymorphism
(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!
==Example== Imagine an operator <code>+</code> that may be used in the following ways: # <code>1 + 2 = 3</code> # <code>3.14 + 0.0015 = 3.1415</code> # <code>1 + 3.7 = 4.7</code> # <code>[1, 2, 3] + [4, 5, 6] = [1, 2, 3, 4, 5, 6]</code> # <code>[true, false] + [false, true] = [true, false, false, true]</code> # <code>"bab" + "oon" = "baboon"</code> To handle these six function calls, four different pieces of code are needed (or three, if strings are considered to be lists of characters): * In the first case, [[integer (computer science)|integer]] addition must be invoked. * In the second and third cases, [[floating point|floating-point]] addition must be invoked (with [[type promotion]], or [[type coercion]], in the third case). * In the fourth and fifth cases, [[List (computing)|list]] [[concatenation]] must be invoked. * In the last case, [[literal string|string]] concatenation must be invoked. Thus, the name <code>+</code> actually refers to three or four completely different functions. This is an example of ''[[Overloading (programming)|overloading]]'' or more specifically, ''[[operator overloading]]''. Note the ambiguity in the string types used in the last case. Consider <code>"123" + "456"</code> in which the programmer might ''naturally'' assume addition rather than concatenation. They may expect <code>"579"</code> instead of <code>"123456"</code>. Overloading can therefore provide different meaning, or semantics, for an operation, as well as differing implementations.
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)