c++: accessing a class static variable

Started by
3 comments, last by NDIR 16 years, 3 months ago
I have a static variable in a class which I use as a counter - it's initialised to zero and incremented in the constructor. I need to check its value in main(), to see if any members of the class exist, but since it's private, I have getters and setters... but they're only accessible through objects of the class... right? Is the only solution to make the variable public?
Advertisement
Functions can be static, too.
so I'd have:

static int get_counter() { return counter; }

?

how would I call that from the program?
The same way you'd access any static from outside the class. You prepend MyClass:: to the name to indicate that you want the name from the class. Ex:
MyClass::get_counter();
use CMyClass::get_counter();

oh, too late :)

This topic is closed to new replies.

Advertisement