R
richardclay09
The output of this:
#include <iostream>
#include <vector>
using namespace std;
struct X {
int i;
X(const X& x) : i(x.i) {
cout << "ctor copy: " << i << endl;
}
X(int ii) : i(ii) {
cout << "ctor by int: " << i << endl;
}
~X() {
cout << "dtor: " << i << endl;
}
};
int main(int argc, char **argv) {
vector<X> v;
v.push_back(X(100));
return 0;
}
Is this:
ctor by int: 100
ctor copy: 100
ctor copy: 100
dtor: 100
dtor: 100
dtor: 100
When you do the push_back, I can understand the vector wanting to make
its own copy, but why is this apparently done twice?
#include <iostream>
#include <vector>
using namespace std;
struct X {
int i;
X(const X& x) : i(x.i) {
cout << "ctor copy: " << i << endl;
}
X(int ii) : i(ii) {
cout << "ctor by int: " << i << endl;
}
~X() {
cout << "dtor: " << i << endl;
}
};
int main(int argc, char **argv) {
vector<X> v;
v.push_back(X(100));
return 0;
}
Is this:
ctor by int: 100
ctor copy: 100
ctor copy: 100
dtor: 100
dtor: 100
dtor: 100
When you do the push_back, I can understand the vector wanting to make
its own copy, but why is this apparently done twice?