c++: accessing a class static variable
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?
so I'd have:
static int get_counter() { return counter; }
?
how would I call that from the program?
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();
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement