18

C/C++: Can I access static variables inside a function from outside? For 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 this is an invalid syntax.
    f();

    return 0;
}
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Robin Hsu
  • 4,164
  • 3
  • 20
  • 37

8 Answers8

18

No, you can't, neither in C nor in C++.

If you want to maintain state associated with a function, define a class with the appropriate state and a member function. (In C++. You've also tagged the question with C; the same technique works but you need to do all the groundwork yourself.)

Although they have their uses, most of the time non-const static locals are a bad idea. They make your function thread-unsafe, and they often make it "call-once".

rici
  • 234,347
  • 28
  • 237
  • 341
10

Variables inside a function scope cannot be accessed externally by name, but you can return a pointer or reference to it

Glenn Teitelbaum
  • 10,108
  • 3
  • 36
  • 80
4

As it was said in other answers, there's no such a thing as a special syntax for accessing local variables externally, but you can achieve this by returning a reference to the variable, for instance:

#include <iostream>
using namespace std;
int& f(void)
{
     static int count = 3;
     cout << count << endl;

     return count;
}

int main()
{
    int &count = f(); // prints 3
    count = 5;
    f();              // prints 5
    return 0;
}

In C++11 you can also access your local static variables from a returned lambda:

#include <iostream>
#include <functional>

using namespace std;

std::function<void()> fl()
{
    static int count = 3;
    cout << count << endl;

    return []{count=5;};
}

int main()
{
    auto l = fl();  // prints 3
    l();
    fl();           // prints 5
    return 0;
}
Tarc
  • 3,214
  • 3
  • 29
  • 41
1

No, The variable count is only available inside function and has no linkage. However the lifetime of this variable would be the scope of the file, as C++ Primer suggests

Each local static variable is initialized before the first time execution passes through the object's definition. Local statics are not destroyed when a function ends; they are destroyed when program terminates.

Tejendra
  • 1,874
  • 1
  • 20
  • 32
0

A static variable has a scope within the block but it has a lifetime of throughout the program so you cant access the coun tvariable try returning a pointer from the function.

Sourav Kanta
  • 2,727
  • 1
  • 18
  • 29
0

No, but (obviously) you can always move it outside of the function so it becomes accessible for the whole file; except for cluttering file-level name space (which is IMHO not a big deal), I don't see much downsides for such a move (and it will be more explicit and obvious too: rather than making access to a supposedly private thing, you'll make it clear that this thing is to be shared).

That being said, all kinds of non-const static data should be used with extreme care, and a rule of thumb (i.e. until proven otherwise) it is that they're a Bad Idea.

No-Bugs Hare
  • 1,557
  • 14
  • 15
  • For a value inside the function, the constructor is called when the function is first called, for a global value outside the function it is created before main is called. This can mean a big difference in behavior in some cases. – Michael Anderson Jun 04 '15 at 06:16
  • @Michael Anderson: Good point, though for int as in original example, it doesn't matter. – No-Bugs Hare Jun 04 '15 at 06:34
0

In C and C++ languages, the scope in which the variable defined is important. You cant reach a variable from upper scopes.

For this purpose you can use structs or C++ classes and keep this data in the objects.

fatihk
  • 7,789
  • 1
  • 26
  • 48
0

No, static variable has its scope limited to block in which it is defined, while its life time is though out the process, so as variable is defined in function it will get into existence once this method is called but in order access it we need to be in function scope.