vector doesnt obey constructor of classess?

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <vector>
using namespace std;

class abc
{
public:
	static int 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;
}
Implement the copy constructor.
Last edited on
Thanks. got it :)
Topic archived. No new replies allowed.