构造函数复制构造赋值操作移动构造移动操作
Posted Kiven#5197
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了构造函数复制构造赋值操作移动构造移动操作相关的知识,希望对你有一定的参考价值。
六个默认函数:
- 构造函数(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
以上是关于构造函数复制构造赋值操作移动构造移动操作的主要内容,如果未能解决你的问题,请参考以下文章