Clang-Tidy:移动构造函数通过调用复制构造函数来初始化类成员
Clang-Tidy: Move constructor initializes class member by calling a copy constructor
class Vtx {
private:
int indexPoint;
int radius;
std::string strName;
public:
Vtx(const int p_indexPoint, const int p_radius, std::string p_strName)
: indexPoint(p_indexPoint),
radius(p_radius),
strName(std::move(p_strName)) {
std::cout << "Constructorn";
}
Vtx(const Vtx& rhs)
: indexPoint(rhs.indexPoint),
radius(rhs.radius),
strName(rhs.strName) {
std::cout << "Copy constructorn";
}
// (#)
Vtx(const Vtx&& rhs)
: indexPoint(rhs.indexPoint),
radius(rhs.radius),
strName(rhs.strName) {
std::cout << "Move constructorn";
}
~Vtx() {
std::cout << "Destructorn";
}
};
对于此代码,clang-tidy 给了我以下警告消息。
- 移动构造函数
- 通过调用复制构造函数初始化类成员
我不明白什么时候在移动构造函数中调用复制构造函数。(#)
虽然编译效果很好,但我想删除这个警告。我该怎么解决?
你忘记移动成员(对于内置类型,无论如何移动都是复制的,可以省略(
并且从 const 对象移动通常是一个副本,因此您必须更改为
Vtx(Vtx&& rhs)
: indexPoint(rhs.indexPoint),
radius(rhs.radius),
strName(std::move(rhs.strName)) {
std::cout << "Move constructorn";
}
请注意,在您的情况下,除了消息(您可以在专用类中移动该代码以Vtx
简化实现 BTW(,默认实现是可以的:
Vtx(Vtx&& rhs) = default;
相关文章:
- 有没有一种代码密度较低的方法来使用非默认构造函数初始化数组?
- 如何在 C++ 中使用它的构造函数初始化 unique_ptrs 的 2D 向量?
- 在C++中使用默认构造函数初始化对象的不同方法
- 使用默认构造函数初始化对象的不同方法
- 我们可以用参数化构造函数初始化结构的数组吗?
- 有没有办法使用该类的构造函数初始化另一个类的私有部分内的对象数组?
- 类内初始化与构造函数初始化列表的顺序
- 通过 C++ 中的重载构造函数初始化未知类型的变量
- 使用复制构造函数初始化 new[]
- 如何从子类的构造函数初始化父类的私有成员
- 在构造函数初始化列表中使用 std::variant
- 使用构造函数初始化结构还是在之后设置其值更好?
- C++ 没有匹配的构造函数初始化 []
- 我正在使用dev c ++,但收到错误(C++98'array'必须由构造函数初始化)
- 使用所述填充构造函数初始化向量中的向量
- 如何同时创建一个具有两个或多个构造函数初始化的对象
- 在 new 关键字中,由默认构造函数初始化的类中的元素是否也使用 new 关键字在C++?
- 这是使用构造函数初始化数组对象的最佳方法吗?
- 内置类型的构造函数初始化
- 构造函数初始化和对象损坏