1#include <iostream>2#include <string.h>3 using namespacestd;4 5 //pretending to write an output class6 classOutput7 {8 Public:9 //operator overload mode oneTenoutput&operator>> (stringb) One { Acout <<b; - return* This; - } the - //operator overloading mode two -Friend output&operator>> (output& out,inti) - { +cout <<"output a integer:"<<i; - return out; + } A atOutputoperator! (void) - { -cout <<"hehe!\n"; - return* This; - } - }; in - intMain () to { +Output out; - stringHello ="Hello, world!\n."; the out>>Hello; * out>>"Hello, world!\n."; $ out>>"Hello, world!\n.">>"Good bye!\n";Panax Notoginseng out= ! out; - the out>>23333>>"\ n"; + Acout <<"Hello World"<<Endl; the return 0; +}
Directly look at the code, C + + operator overloading has two implementations, one is overloaded into a member of the class function, the other is the overload as a friend function, the member function has the this pointer, so one less parameter.
Attach operator Precedence
Priority Level |
operator |
Description |
Example |
Binding Nature |
1 |
() [] - . :: ++ -- |
The parentheses operator that adjusts the precedence Array Subscript access operator To access a member's operator by pointing to the object's pointer Operator that accesses members through the object itself Scope operator Post increment operator Post-decrement operator |
(A + B)/4; ARRAY[4] = 2; Ptr->age = 34; Obj.age = 34; Class::age = 2; for (i = 0; i <; i++) ... for (i = ten; i > 0; i--) ... |
From left to right |
2 |
! ~ ++ -- - + * & (type) sizeof |
Logical take-back operator bitwise inverse (bitwise complement) Predecessor increment operator Pre-decrement operator unary minus operator Unary take positive operator Dereference operator Fetch address operator Type conversion operators Returns the number of bytes occupied by an object operator |
if (!done) ... Flags = ~flags; for (i = 0; i <; ++i) ... for (i = ten; i > 0; i.) ... int i =-1; int i = +1; data = *ptr; Address = &obj; int i = (int) floatnum; int size = sizeof (floatnum); |
From right to left |
3 |
->* .* |
Operator that accesses a member on a pointer through a pointer to a member Operator that accesses a member on an object by pointing to a pointer to a member |
Ptr->*var = 24; Obj.*var = 24; |
From left to right |
4 |
* / % |
Multiplication operator Division operator Take remainder operator |
int i = 2 * 4; float F = 10/3; int rem = 4% 3; |
From left to right |
5 |
+ - |
Addition operator Subtraction operator |
int i = 2 + 3; int i = 5-1; |
From left to right |
6 |
<< >> |
Bitwise left SHIFT operator Bitwise RIGHT SHIFT operator |
int flags = << 1; int flags = >> 1; |
From left to right |
7 |
< <= > >= |
Less than comparison operators Less than or equal to comparison operators Greater than comparison operator Greater than or equal to comparison operator |
if (I < 42) ... if (i <= 42) ... if (i > 42) ... if (i >= 42) ... |
From left to right |
8 |
== != |
equals comparison operator Not equal to comparison operator |
if (i = = 42) ... if (i! = 42) ... |
From left to right |
9 |
& |
Bitwise AND operator |
Flags = flags & 42; |
From left to right |
10 |
^ |
Bitwise XOR OR operator |
Flags = flags ^ 42; |
From left to right |
11 |
| |
Bitwise OR operator |
Flags = Flags | 42; |
From left to right |
12 |
&& |
Logic and operators |
if (Conditiona && conditionb) ... |
From left to right |
13 |
|| |
Logical OR operator |
if (Conditiona | | conditionb) ... |
From left to right |
14 |
? : |
Ternary conditional operator |
int i = (a > B)? A:B; |
From right to left |
15 |
= += -= *= /= %= &= ^= |= <<= >>= |
Assignment operator Compound assignment Operator (addition) Compound assignment operator (subtraction) Compound assignment operator (multiplication) Compound assignment Operator (division) Compound assignment operator (take surplus) Compound assignment operators (bitwise AND) Compound assignment operator (bitwise XOR) Compound assignment operator (bitwise OR) Compound assignment operator (bitwise left shift) Compound assignment operator (bitwise RIGHT SHIFT) |
int a = b; A + = 3; B-= 4; A *= 5; A/= 2; A%= 3; Flags &= new_flags; Flags ^= new_flags; Flags |= new_flags; Flags <<= 2; Flags >>= 2; |
From right to left |
16 |
, |
Comma operator |
for (i = 0, j = 0; i <; i++, J + +) ... |
From left to right |
Operators that cannot be overloaded
Only existing C + + operators can be overloaded in C + +, and users are not allowed to define new operators themselves!
Most of the operators in C + + can be overloaded, except for member access operators. The member pointer access operator . * , scope operator :: , length operator sizeof , and conditional operator ?: c4>;
Reference: http://www.cnblogs.com/wiessharling/p/4179753.html
C + + operator overloading