Static members

Hi, as a beginner of C++ I found the C++ Language Tutorial on this site is extremely helpful. As I progress through the tutorial, I seemed to get stuck at the Classes (II) section with these lines of code: (the output is 7,6)


// 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;
}

Would anyone want to explain?

Thank you.
A static variable exists just once during the life time of the program.

What 'CDummy' does with n is basically count how many objects of that type exists.
Thanks for your input.

I re-read that section again and figured it out. CDummy a generated n=1, ..b[5].. n= 1+ 5, and c.. n=7. Delete c generated n= 6!!!

Topic archived. No new replies allowed.