![C++ Basic Skills: Lesson 22 C++ Basic Skills: Lesson 22](http://web.archive.org./web/20110205193234im_/http://i.ytimg.com/vi/hiog9Y-683w/0.jpg)
- Order:
- Duration: 11:00
- Published: 2009-08-29
- Uploaded: 2011-01-19
- Author: outofmylaboratory
- http://wn.com/C++_Basic_Skills_Lesson_22_Operator_Overloading_part_1_of_2
- Email this video
- Sms this video
these configurations will be saved for each time you visit this page using this browser
Operator overloading is claimed to be useful because it allows the developer to program using notation "closer to the target domain" and allows user-defined types a similar level of syntactic support as 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))
Addition is a binary operation, which means it has left and right operands. In C++, the arguments being passed are the operands, and the temp
object is the returned value.
The operation could also be defined as a class method, replacing lhs
by the hidden this
argument; however this forces the left operand to be of type Time
and supposes this
to be a potentially modifiable lvalue:
Note that a unary operator defined as a class method would receive no apparent argument (it only works from this
):
<<
in C++'s:
The common reply to this criticism 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 wrongly expected or unintentionally assumed. For example the commutativity of + (i.e. that 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. "school" + "bag"
yields "schoolbag"
, which is different from "bag" + "school"
yields "bagschool"
). A typical counter to this argument comes directly from mathematics: While + is commutative on integers (and in general any real numbers), it is not commutative for other "types" of variable. It can be further noted that + is not even associative on floating point values in practice due to rounding errors. Another example: binary * (multiplication) is commutative for integers but not commutative in case of matrix multiplication.
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.
C++'s operator overloading is further refined from that of ALGOL 68's.
Category:Articles with example ALGOL 68 code Category:Programming constructs
This text is licensed under the Creative Commons CC-BY-SA License. This text was originally published on Wikipedia and was developed by the Wikipedia community.