11 destructor and 2 constructor for 5 objects???
#include <iostream> #include <vector> using namespace std; class A { public: static int cnt; int a; A(int a) { A::a = a; cout << "I am a " << cnt << " constructor!" << endl; } ~A() { ++cnt; cout << "I am a " << cnt << " destructor!" << endl; } }; int A::cnt = 0; int main() { vector<A> v(4, 1); v.push_back(1); cout << A::cnt << endl; return 0; } This code ouputs: I am a 0 constructor! I am a 1 destructor! I am a 1 constructor! I am a 2 destructor! I am a 3 destructor! I am a 4 destructor! I am a 5 destructor! I am a 6 destructor! 6 I am a 7 destructor! I am a 8 destructor! I am a 9 destructor! I am a 10 destructor! I am a 11 destructor! Why do we have so many destructors and only 2 constructors??