提问人:Amir reza Riahi 提问时间:7/22/2022 最后编辑:Amir reza Riahi 更新时间:7/22/2022 访问量:52
为什么不调用移动构造函数?[复制]
Why the move constructor isn't invoked? [duplicate]
问:
这个问题在这里已经有答案了:
存储重载运算符的结果时不调用复制/移动构造函数+ (1 个答案)
c++: MyClass x(1,2,3) vs MyClass x = MyClass(1,2,3) [duplicate] (1 个答案)
Radio r = Radio(“PSR”, 100.8) 和 Radio(“PSR”, 100.8) 有什么区别?[复制] (1 个回答)
两个结构之间有区别 吗 (1 个答案)
去年关闭。
我定义了一个同时具有复制和移动构造函数的类。复制构造函数似乎工作正常,但是当我尝试调用移动构造函数时,它不起作用。
#include <iostream>
class A{
public:
A() = default;
A(A& a){
std::cout << "Copy constructor." << std::endl;
};
A(A&& a){
std::cout << "Move constructor." << std::endl;
};
};
int main(int argc, char *argv[]){
auto a = A(A());
std::cout << "here" << std::endl;
auto b = A(a);
return 0;
}
我希望应该调用移动构造函数,因为它的输入()是一个.但输出是这样的:auto a = A(A());
A()
rvalue
[amirreza@localhost tmp]$ ./a.out
here
Copy constructor.
我的假设错了吗?
我正在使用版本而没有明确指定 c++ 版本。gcc
10.3.1
答: 暂无答案
评论
const
auto a = A(A());
auto b = A(a);