环境:VS2022 编译器做了太多的优化。
下面push_back两次,但是运行结果显示有三次的拷贝构造,第二次push_back不知为何生成了临时对象。
这是个语法细节 也希望大家不必陷入这种语法细节的泥沼。
class Test
{
public:
Test()
{
cout << "无参构造" << endl;
}
Test(const Test&)
{
cout << "拷贝构造" << endl;
}
Test(Test&&)
{
cout << "移动构造" << endl;
}
~Test()
{
cout << "析构" << endl;
}
};
int main()
{
Test test;//左值
cout << endl;
std::vector<Test>v1;
cout << "push_back:";
v1.push_back(test);
v1.push_back(test);
return 0;
}