构造函数、复制构造、赋值操作、移动构造、移动操作

 六个默认函数:

  1. 构造函数(construct)
  2. 析构函数(destruct)
  3. 复制构造函数(copy construct)
  4. 赋值(assign)
  5. 移动构造函数(move construct)
  6. 移动赋值(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

https://www.cnblogs.com/lustar/p/7512198.html

http://www.cnblogs.com/BlueTzar/articles/1223313.html

转载于:https://www.cnblogs.com/Kiven5197/p/8877213.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值