"Turn" http://www.cnblogs.com/zi-xing/p/4590282.html
The role of Static
- In the function body, a variable declared static, the value of which is maintained in the process of the function being called.
- Within the module (but outside the function body), a declared static variable can be accessed by all functions within the module, but cannot be accessed by a function outside the module, which is a local global variable
- Within the module, a function declared as static can only be called by other functions within the module, which is used within the module that declares it.
In general, static controls the scope of use of variables and functions (Local data and code range control)
The difference between a static global variable and a normal global variable
Both global variables and static global variables are static storage methods. The difference is that the scope of the non-static global variable is the whole source program, when a source program has multiple source files, non-static global variables are valid in each source file, while static global variables limit the scope, only the source file that defines the static global variable is valid, the other source files of the same source program cannot use it. Use static global variables to avoid causing errors in other source files.
In summary: Changing the local variable to a static variable changes its storage mode, and changes its lifetime, changing the global variable to a static variable, changing its scope and limiting its scope of use.
- Static global variables differ from normal global variables: Static global variables are initialized only once, preventing references in other file units
- Static local variables differ from ordinary variables: Static global variables are initialized only once, and next time based on the last result value.
- The difference between a static function and a normal function: The static function has only one copy in memory, and the normal function maintains a copy of each call.
C + + static notes