DiemazzBlood II: The ChosenNo More Heroes (video game) Aimee Chan Borama script HichisÅ, Gifu Arcadia, Oklahoma Category:Cities, towns and villages in the Est Region Charles X Hand, foot and mouth disease Quezon History of Mauritius Illinois Jacquet Rail shooter Psycholinguistics Amphoe Noen Maprang national coalition against censorship alliance Agh (trigraph) Minamiaizu District, Fukushima play with your food meal Bell Centre Sheer Heart Attack Paak Ingria (TO) Nicolas Farina Macherio John William III, Duke of Saxe Eisenach Torbay Image:Commons logo svg Bourberain Mathilda of Tuscany Rossøya Slovenian rock Japanese destroyer Urakaze La Celle sous Chantemerle Amanda Burton File:JRW 321 x2 Osaka jpg Blay JALways File:Wychk new annex jpg Kingston, Jamaica Turboprop Egyptian chronology File:Harem entrance Topkapi Istanbul 2007 jpg Mitsue, Nara Extraterritoriality File:Gran Vía (Madrid) 15 jpg Category:Companies listed on the Toronto Stock Exchange Hostage gokudo characters Keihan Electric Railway Bangsar Sakamoto Station (Miyagi) Advanced Power Management Frederick William III of Prussia Paul Masvidal |
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 useful because it allows the developer to program using notation closer to the target domain and allows user types to look like 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)) However, in C++ templates or even C macros, operator overloading is needed in writing down generic, primitive operations such as summing elements when implementing a vector template. The only way to write primitive addition is A more general variant of operator overloading, called expression reduction, allows expressions containing one or multiple operators to be reduced to a single function call. The expression above could be reduced to: operator_add_and_multiply_integers(a, b, c)
CriticismsOperator 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
a << 1
shifts the bits in the variable a left by 1 bit if a is of an integer type, but if a is an output stream then the above code will attempt to write a "1" 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 usually considered good practice to use operator overloading with care. Changing the semantics can even happen by accident, as a common pitfall in C++ operator overloading is confusion between the arithmetic operators and assignment operators: Beginners frequently overload the addition (+) operator but give it the semantics of the assignment by addition (+=) operator, resulting in simple expressions like The common reply to this criticism, given by programmers who favor operator overloading, is that the same argument applies to function overloading as well. Furthermore, even in the absence of overloading, a programmer can define a function to do something totally different from what would be expected from its name. An issue that remains is that languages such as C++ provide a limited set of operator symbols, thus removing from programmers the option of choosing a more suitable operator symbol for their new operation. Another, more subtle issue with operators is that certain rules from mathematics can be expected or unintentionally assumed. For example the commutativity of + (i.e. that Operators are overloaded so that the objects behave as primitive types. New operators cannot be created, only the functionality of existing operators on objects can be modified; at least in C++. A particular problem from the aspect of performance, is that operator overloading can describe nothing about the relationships between the operators. For instance, it is the case that for an unsigned integer CatalogA 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.
Notes:
Timeline of operator overloading1960sThe ALGOL 68 specification allowed operator overloading[1]. Extract from the ALGOL 68 language specification (page 177) where the overloaded operators ¬, =, ≠ and abs are defined: 10.2.2. Operations on Boolean Operands a) op ∨ = (bool a, b) bool:( a | true | b ); b) op ∧ = (bool a, b) bool: ( a | b | false ); c) op ¬ = (bool a) bool: ( a | false | true ); d) op = = (bool a, b) bool:( a∧b ) ∨ ( ¬b∧¬a ); e) op ≠ = (bool a, b) bool: ¬(a=b); f) op abs = (bool a)int: ( a | 1 | 0 ); Note that no special declaration is required to overload an operator, and the programmer is free to create new operators. 1980sAda supported overloading of operators from its inception with the publication of the Ada 83 language standard. However, the designers of the language consciously chose not to permit the definition of new operators: only the existing operators in the language may be overloaded (by defining new functions with identifiers such as "+", "*", "and" etc.). Subsequent revisions of the language (in 1995 and 2005) have maintained the restriction to overloading of existing operators. C++'s operator overloading was further refined from that of ALGOL 68's. [2] 1990sSun deliberately chooses not include operator overloading in the Java language. (cite: http://www.cafeaulait.org/javafaq.html#xtocid1902938) 2001Microsoft includes operator overloading in C#. Notes and references
See also
|
Site Map: RSS 2.0
Recent Searches:
Psychic detective
Related Pages: |