static means slight different things in different places, but in all cases, it is similar to a global variable in the sense that it exists for the lifetime of the program.
Myfile.cpp:
1 2 3 4 5 6 7 8 9 10 11
|
static int fileStatic;
class MyClass {
static int classStatic;
};
int MyClass::classStatic;
void func()
{
static int funcStatic;
};
| |
Here fileStatic, classStatic and funcStatic are all single variables. There is exactly one of each. They last for for the lifetime of the program. The only difference is in who can see them.
fileStatic
is visible only to code within MyFile.cpp
classStatic
is within the name space of MyClass (apologies for not having the terminology right here. I hope someone will correct me).
classStatic
's depends on whether it is private, protected or public.
funcStatic
is only visible within func().
In summary, the difference between global and static variables is their visibility.
[There is one subtle difference: function statics are initialized the first time the function is called rather than before main() runs.]