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

 六个默认函数:

  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

posted @ 2018-04-18 19:56  Kiven#5197  阅读(610)  评论(0编辑  收藏  举报