c++ - 隐式移动构造函数
问题描述
隐式移动构造函数到底在做什么?例如,以下类的隐式移动构造函数将如何(您能否提供此隐式构造函数的一些示例实现):
struct A
{
A() = default;
A(A && other) = default;
int a;
};
struct B : public A
{
int b;
int * c;
};
实现会像这样:
B(B && other) : A(std::move(other)), b(std::move(other.b)), c(std::move(other.c)) {}
解决方案
来自 cppreference.com:
对于联合类型,隐式定义的移动构造函数复制对象表示(如 std::memmove)。对于非联合类类型(类和结构),移动构造函数使用带有 xvalue 参数的直接初始化,按照 初始化顺序对对象的基类和非静态成员执行完整的成员移动。如果这满足 constexpr 构造函数的要求,则生成的移动构造函数是 constexpr。
基类构造函数在派生构造函数之前运行。
推荐阅读
- javascript - 无法运行剧作家“写作断言”示例
- python - 根据另一个字段更改 Django 数组字段大小
- linux - 背页社区版 (Sharelatex) | 如何从磁盘读取更改后的 LaTex 文件?
- bundler - 捆绑安装 | rdkafka 0.8.1 错误:版本字符串为空
- airflow - 如何将先前的任务状态作为参数传递给 Airflow Taskflow API 中的另一个任务?
- python - 使用 Python 进行网络抓取时获取日期选择器值的问题
- firebase - 如何在 Firestore 查询中使用 arrayContains 和 not-in 过滤器?
- python - 为什么我会得到无效的文字 python3
- python - 如何使用 matplotlib 获得不同颜色的不同小提琴?
- java - 当 vpn 连接但网络断开时,网络功能显示为真