Generally, to be safe, you implement these kinds of "instance counters" wrapped in a factory function. Typically, you also don't want spurious copies and things to be created, so you make the class non-copyable. This way you count only the "uniquely new" objects that were created, not copies and temporaries. By making the class non-copyable, you force it to be used via a pointer, and the only function to create that pointer is your factory function, which records the instances created:
class People {
private:
int ssn;
People(int aSSN) : ssn(aSSN) { }; //constructible only from factory Create().
~People() { }; //destructible only from factory Destroy().
People(const People); //non-copyable.
People& operator=(const People&); //non-assignable.
public:
static People* Create() {
static int objectCount = 0;
return new People( ++objectCount );
};
static void Destroy(People* p) { delete p; };
};
int main() {
People p1 = People::Create();
People::Destroy(p1);
return 0;
};