1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
#include <iostream> using namespace std;
class Integer {
long i;
Integer* This() { return this ; }
public :
Integer( long ll = 0) : i(ll) {}
// No side effects takes const& argument:
friend const Integer&
operator+( const Integer& a);
friend const Integer
operator-( const Integer& a);
friend const Integer
operator~( const Integer& a);
friend Integer*
operator&(Integer& a);
friend int
operator!( const Integer& a);
// Side effects have non-const& argument:
// Prefix:
friend const Integer&
operator++(Integer& a);
// Postfix:
friend const Integer
operator++(Integer& a, int );
// Prefix:
friend const Integer&
operator--(Integer& a);
// Postfix:
friend const Integer
operator--(Integer& a, int );
}; // Global operators: const Integer& operator+( const Integer& a) {
cout << "+Integer\n" ;
return a; // Unary + has no effect
} const Integer operator-( const Integer& a) {
cout << "-Integer\n" ;
return Integer(-a.i);
} const Integer operator~( const Integer& a) {
cout << "~Integer\n" ;
return Integer(~a.i);
} Integer* operator&(Integer& a) { cout << "&Integer\n" ;
return a.This(); // &a is recursive!
} int operator!( const Integer& a) {
cout << "!Integer\n" ;
return !a.i;
} // Prefix; return incremented value const Integer& operator++(Integer& a) {
cout << "++Integer\n" ;
a.i++;
return a;
} // Postfix; return the value before increment: const Integer operator++(Integer& a, int ) {
cout << "Integer++\n" ;
Integer before(a.i);
a.i++;
return before;
} // Prefix; return decremented value const Integer& operator--(Integer& a) {
cout << "--Integer\n" ;
a.i--;
return a;
} // Postfix; return the value before decrement: const Integer operator--(Integer& a, int ) {
cout << "Integer--\n" ;
Integer before(a.i);
a.i--;
return before;
} int main() {
Integer a;
+a;
-a;
~a;
Integer* ip = &a;
!a;
++a;
a++;
--a;
a--;
return 0;
} ///:~
|
本文转自FrankNie0101 51CTO博客,原文链接:http://blog.51cto.com/frankniefaquan/1934028,如需转载请自行联系原作者