When I use vectors to create multiple objects of a class, why doesn't it update a static counter of the class which I do in the constructor? But When i empty the vector, destructor executes properly and decrements the static count variable. How can I keep track of the objects created of a particular class without explicitly counting at all places of the code?
#include <iostream>
#include <vector>
usingnamespace std;
class abc
{
public:
staticint count;
int data;
abc(){count++;}
~abc(){count--;}
};
int abc::count;
int main ()
{
abc var;
cout<<abc::count<<endl; // prints 1
abc vars[5];
cout<<abc::count<<endl; // prints 6
vector<abc> vec(5); // the count variable is not incremented
for(int i=0;i<5;i++)vec[i].data=i;
cout<<abc::count<<endl; // prints again as 6 :(
vec.clear();
cout<<abc::count<<endl; // prints 1
return 0;
}