构造函数、复制构造、赋值操作、移动构造、移动操作
六个默认函数:
- 构造函数(construct)
- 析构函数(destruct)
- 复制构造函数(copy construct)
- 赋值(assign)
- 移动构造函数(move construct)
- 移动赋值(move)
1 #include <iostream>
2
3 using namespace std;
4
5 int g_constructCount = 0;
6 int g_copyConstructCount = 0;
7 int g_destructCount = 0;
8 int g_moveConstructCount = 0;
9 int g_assignCount = 0;
10 int g_moveCount = 0;
11
12 struct A
13 {
14
15 A()
16 {
17 cout << "construct:" << ++g_constructCount << endl;
18 }
19
20 A(const A& a)
21 {
22 cout << "copy construct:" << ++g_copyConstructCount << endl;
23 }
24
25 A(A&& a)
26 {
27 cout << "move construct:" << ++g_moveConstructCount << endl;
28 }
29
30 ~A()
31 {
32 cout << "destruct:" << ++g_destructCount << endl;
33 }
34
35 A& operator=(const A& other)
36 {
37 cout << "assign:" << ++g_assignCount << endl;
38 return *this;
39 }
40 A& operator=(A&& a)
41 {
42 cout << "move:" << ++g_moveCount << endl;
43 return *this;
44 }
45 };
参考:
https://blog.csdn.net/jofranks/article/details/17438955