1 #include <iostream> // 标准的输入输出
2 using namespace std;
3
4 class MyInteger {
5 public:
6 MyInteger()
7 {
8 ¦ m_A = 0;
9 }
10 int m_A;
11 };
12
13 // 前置++重载
14 MyInteger& operator++(MyInteger& p)
15 {
16 p.m_A++;
17 return p;
18 }
19
20 //后置++重载
21 MyInteger operator++(MyInteger& p, int)
22 {
23 MyInteger tmp=p;
24 p.m_A++;
25 return tmp;
26 }
27 // 重载<<运算符
28 ostream& operator<<(ostream& cout, MyInteger& m)
29 {
30 cout << m.m_A;
31 return cout;
32 }
33
34 void test01()
35 {
36 MyInteger myint;
37 // 前置++重载
38 cout << ++myint << endl;
39 // 后置++重载
40 myint++;
41 cout << myint << endl;
42 }
43 int main()
44 {
45 test01();
46 return 0;
47 }
c++ ++运算符重载
最新推荐文章于 2024-05-02 00:37:43 发布