C++11 类的六个默认函数及其使用
来源:互联网 发布:淘宝自动充值平台 编辑:程序博客网 时间:2024/06/07 14:18
六个默认函数:
- 构造函数(construct)
- 析构函数(destruct)
- 复制构造函数(copy construct)
- 赋值(assign)
- 移动构造函数(move construct)
- 移动赋值(move)
测试代码:
#include <iostream>using namespace std;int g_constructCount = 0;int g_copyConstructCount = 0;int g_destructCount = 0;int g_moveConstructCount = 0;int g_assignCount = 0;int g_moveCount = 0;struct A{ A() { cout << "construct:" << ++g_constructCount << endl; } A(const A& a) { cout << "copy construct:" << ++g_copyConstructCount << endl; } A(A&& a) { cout << "move construct:" << ++g_moveConstructCount << endl; } ~A() { cout << "destruct:" << ++g_destructCount << endl; } A& operator=(const A& other) { cout << "assign:" << ++g_assignCount << endl; return *this; } A& operator=(A&& a) { cout << "move:" << ++g_moveCount << endl; return *this; }};
测试:
情形一:A a等价于A a=A();
情形二:
{ A a ; a = A();//A()为右值,所以move}结果:construct:1construct:2move:1destruct:1destruct:2
情形三: A a,b; a=b;//b为左值,所以assign
情形四:
{ A a; A c(a);}结果:construct:1copy construct:1destruct:1destruct:2
函数参数传递:
void fun(A a){ cout << "funA" << endl;}
情形一:
{ A a; fun(a);}结果:construct:1copy construct:1funAdestruct:1destruct:2
情形二:
{ A a; fun(move(a));}结果:construct:1move construct:1funAdestruct:1destruct:2
情形三:
{ fun(A());}结果:construct:1funAdestruct:1
0 2
- C++11 类的六个默认函数及其使用
- C++类的六个默认成员函数
- 类的六个默认成员函数
- 一个类的六个默认函数
- 类的六个默认成员函数
- 类的六个默认成员函数
- 浅谈类的六个默认的成员函数
- 详解c++中类的六个默认的成员函数
- 类的六个默认函数及运算符的重载
- 详解c++中类的六个默认的成员函数
- 详解c++中类的六个默认的成员函数
- 类的六个默认成员函数总结(一)
- 类的六个默认成员函数(二)
- 类的六个默认成员函数(转)
- C++之类的六个默认成员函数
- c++默认的六个成员函数
- c++类中六个默认成员函数
- 浅谈类的六个默认成员函数和隐含的this指针
- python挑战题(转+个人解答)
- 设计模式之简单工厂模式
- TQ2440 学习笔记—— 27、移植U-Boot【Bootloader 作用、工作流程】
- 2015 九月份学习记录
- 安全软件启动先导保护-各模块流程图[待完善]
- C++11 类的六个默认函数及其使用
- dos 常见的命令行
- 面试题:如何引用一个已经定义过的全局变量?
- Android之TabHost用法—定义Tab标签样式
- 【HDU3829】【二分匹配】【最小独立集】【问啥建啥】
- Windows Socket 编程_ 简单的服务器/客户端程序
- HTML5 标签
- 微信转发指定的图文消息到朋友圈(JAVA版)
- hdu 2571 命运(dp)