1 #include <iostream>
2 using namespace std;
3
4 class Good
5 {
6 public:
7 int member;
8 Good(int a): member(a)
9 {
10 cout << "调用构造" << endl;
11 cout << "member =" << member;
12 }
13 void show()
14 {
15 cout << member << endl;
16 }
17 Good operator+(const Good& obj)
18 {
19 return Good(member - obj.member);
20 }
21
22 friend ostream& operator<<(ostream& os, Good& obj)
23 {
24 os << obj.member << "运算符重载" << endl;
25
26 return os; //这句话关键,便于重复调用
27 }
28
29 Good& operator++()
30 {
31 member++;
32 return *this;
33 }
34
35 Good operator++(int)
36 {
37 //下面的注释部分是本函数实现的另一种写法
38 Good old(*this);
39 member++;
40 return old;
41 }
42
43 // friend Good operator++(Good& obj, int)
44 // {
45 // Good old(obj);
46 // obj.member++;
47 //
48 // return old;
49 // }
50 };
51
52 int main(int argc, char *argv[])
53 {
54 Good a(10), b(20);
55 Good c = a + b;
56 cout << c;
57 ++c;
58 cout << c;
59 c++;
60 cout << c;
61 c.show();
62 return 0;
63 }