c++ - 复制和移动构造函数
问题描述
具有以下代码:
#include <iostream>
#include <vector>
using namespace std;
class CIntPtr {
public:
int* ptr;
public:
CIntPtr() {
// Default constructor
cout << "Calling Default constructor\n";
ptr = new int;
}
CIntPtr(const CIntPtr& obj) {
// Copy Constructor
// copy of object is created
this->ptr = new int;
// Deep copying
cout << "Calling Copy constructor\n";
}
CIntPtr(CIntPtr&& obj) {
// Move constructor
// It will simply shift the resources,
// without creating a copy.
cout << "Calling Move constructor\n";
this->ptr = obj.ptr;
obj.ptr = NULL;
}
~CIntPtr() {
// Destructor
cout << "Calling Destructor\n";
delete ptr;
}
};
CIntPtr returnCIntPtr(CIntPtr a) {
*a.ptr += 2;
return a;
}
int main() {
CIntPtr foo;
returnCIntPtr(foo);
return 0;
}
调用了复制和移动构造函数。为什么还要调用move ctor?既然我们将左值作为 f-ction 参数传递,它不应该只是调用的复制 ctor吗?什么时候可以在这段代码中调用 move ctor?
解决方案
在 function 的return 语句(即)中调用 move 构造return a;
函数returnCIntPtr
。
从局部变量和参数自动移动
如果表达式是一个(可能是带括号的)id 表达式,它命名一个变量,其类型是
- 非易失性对象类型或
a non-volatile rvalue reference to object type (since C++20)
并且声明了该变量
- 在体内或
- 作为参数
最里面的封闭函数或 lambda 表达式,
然后执行两次重载决议以选择用于初始化返回值的构造函数:
or, for co_return, to select the overload of promise.return_value() (since C++20)
a
将首先尝试将其作为 rvalue-expression 复制到返回值,然后选择移动构造函数。
推荐阅读
- arduino - 使用树莓派和传感器计算汽车的速度
- c# - 知道消息来自哪个订阅,在相同的视图模型中,相同的类型
- php - JQuery Post返回值包含空格问题
- synchronization - 为除少数几个目录之外的所有目录解决有利于一个根目录的冲突
- android-studio - 如何修复“在Android中发现多个文件与操作系统无关路径'META-INF/DEPENDENCIES'错误
- javascript - 如何持久化在 node_modules 中所做的更改?
- android - 字符串格式转换为 ? 通过数据绑定
- vue.js - 使用 vue 时如何为多页应用程序设置路径?
- jquery - 如何在 jquery 中使用每个 laravel 和 ajax
- java - 如果对象具有相同的 id,则遍历列表并添加属性