Here is the code:
Code://file main.cc #include "test.h" int main(){ cout<<x; }Code://file test.h static int x = 10;
This code works. But should static int x be local to test.h only? How come it can be used in a totally different file?
Here is the code:
Code://file main.cc #include "test.h" int main(){ cout<<x; }Code://file test.h static int x = 10;
This code works. But should static int x be local to test.h only? How come it can be used in a totally different file?
Do not define variables in header files. Define them in source files, ie .cpp files instead.
Yes, if you define a global variable with the "static" keyword, it means it will only be visible in the current source file. This can allow for some more compiler optimizations.
It can be used in a totally different file, but then it would be a distinct variable. So for example &x will not be the same, and if you change x in one file, it will not change in the other.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.