The whole purpose of static is to declare that a variable is closed to the source file that is declared in . Thus, he does exactly his job of preventing connections to the outside.
Keep in mind that there are four options for defining a file's scope variable:
int blah = 0; - blah is defined in this file and is accessible from other files. Definitions in other files are duplicates and lead to errors.extern int blah; - blah must be defined elsewhere and reference this file.int blah; - This is the moral equivalent of FORTRAN COMMON . You can have any number of them in files, and all of them are allowed by the linker for one common int . (*)static int blah; (optional with an initializer) - this is static. It is completely closed to this file. It is not visible to external files in other files, and you may have many different files that declare static TYPE blah; and they are all different .
For purists in the audience: 'file' = compilation unit.
Note that static internal functions (not in the file area) are even more severely limited: if two functions declare static int bleh = 0; even in one file, they are not connected.
(*): for those of you who are not familiar: in a regular template, one compilation unit should define a global variable, while others may refer to it. He "lives" in this unit of compilation. In case (3) above, not a single file (or all files) defines it. If two files say int blah = 0; , the linker will complain about several definitions. If two files say int blah; , the linker cheerfully creates a single global int and makes all the code reference it.
bmargulies May 15 '10 at 21:10 2010-05-15 21:10
source share