c++ - Can I access static variables inside a function from outside -
c/c++: can access static variables inside function outside? example:
#include <iostream> using namespace std; void f() { static int count = 3; cout << count << endl; } int main(int argc, char** argv) { f::count = 5; // apparently invalid syntax. f(); return 0; }
no, can't, neither in c nor in c++.
if want maintain state associated function, define class appropriate state , member function. (in c++. you've tagged question c; same technique works need groundwork yourself.)
although have uses, of time non-const static locals bad idea. make function thread-unsafe, , make "call-once".
Comments
Post a Comment