When pushing two elements into vector, I think it should be using copy operator twice. And the destructor is used when an object deconstructs. However, the result shows that it uses copy constructor for three times and immediately uses destructor once after the pushing the objects. So what is the reason for this question ?
using namespace std;
struct X {
X() { cout << "X()" << endl; }
X(const X&) { cout << "X(const X&)" << endl; }
X& operator=(const X& xr) { cout << "operator=(const X& xr)" << endl; return *this; }
~X() { cout << "~X()" << endl; }
};
void fcn1(X x1, X &x2, X *x3) {
cout << "fcn1 start" << endl;
vector<X> v1;
cout<<"push x"<<endl;
v1.push_back(x1);
v1.push_back(x2);
cout << "fcn1 end" << endl;
}