我刚刚为移动构造函数编写了一个代码,它被再次调用为传递给 pushback 方法的值 2
#include <iostream>
#include <vector>
using namespace std;
class n{
int *ptr;
public:
n(int d):ptr{new int(d)}{cout<<"constuctor called for "<<d<<endl;
}
n(n &&s)noexcept:ptr{s.ptr}{
s.ptr=nullptr;
cout<<"move constructor called for "<<*ptr<<endl;}//this is being called again for 2
~n(){
if(ptr==nullptr)
cout<<"freeing storage for nullptr "<<ptr<<endl;
else
cout<<"freeing storage for ptr "<<*ptr<<endl;
delete ptr;
}
};
int main() {
vector<n>b;
b.push_back(n{2});
b.push_back(n{3});;
return 0;
}