I'm trying to understand how static ints work within classes, and I've fallen on some code that I don't quite understand how the answers come out to be what they are showing.
// static members in classes
#include <iostream>
using namespace std;
class CDummy {
public:
static int n;
CDummy () { n++; };
~CDummy () { n--; };
};
int CDummy::n=0;
int main () {
CDummy a;
CDummy b[5];
CDummy * c = new CDummy;
cout << a.n << endl;
delete c;
cout << CDummy::n << endl;
return 0;
}
Output is:
7
6
How in the world, are they getting these results!?! I see that n=0 and a.n refers to that value (static int). Is b[5] and array that holds five values? If so, how does "a.n" become 7? And how then, does CDummy::n = 6?