class A
{
int id;
static int count;
public:
A()
{
count++;
id = count;
cout << "constructor called " << id << endl;
}
~A()
{
//count -=2; /*Keypoint is here. */
/*Uncomment it later. But result doesn't change*/
cout << "destructor called " << id << endl;
}
};
int A::count = 0;
int main()
{
A a[2];
return 0;
}
The output is
constructor called 1
constructor called 2
destructor called 2
destructor called 1
The question is:
even if you uncomment the //count -=2;
the result is still the same.
Does that mean that if the constructor increments the static member by 1,then the destructor must decrement it exactly by 1 also, and you can’t change the behaviour of it?
Nothing accesses
countafter the first destructor is invoked. The destructor does exactly what you code it to do, either modifyingcountor not. But you won’t see the effect unless you accesscountin some way.