c++怎样让返回对象的函数不调用拷贝构造函数
我们知道拷贝构造函数有两种“默默”的方式被调用
1. 想函数传入 值参数
2. 函数返回 值类型
今天我们讨论函数返回值类型的情况。
得到结论是
1. 当对象有拷贝构造函数(系统为我们生成、或者我们自己写拷贝构造函数)可以被隐式调用时,函数返回时会使用拷贝构造函数。
2. 当对象的拷贝构造函数声明成为explicit(不能被隐式调用时),函数返回时会使用move构造函数。
先看开始的代码。
explicit Thing(Thing& other):member_(other.member_){//拷贝构造函数cout << "copy" << endl;}/*in method 003EFC18moveout method 003EFD0Cin method -842150451out method -842150451请按任意键继续. . .*/
哈哈,copy->move!!