I. Introduction
Const modified type of memory itself is saying about the value, and Static modified type of memory is saying about the life cycle
Only for functions all use this document to declare the static keyword, this is a good coding style
(1) to other hidden source files
(2) maintaining lasting memory variables, be unique, only a memory
(3 ) The default is initialized to 0Note: The parameter can not be modified static
- rule:
Rule 1: ordinary local variables spent on the release, so many times when we need to use a different variable data, you can use static variables to retain this data
II. For C code
(1) modifying local variable
Keep the memory of long-lasting (be unique, only a memory)
Rule 1: ordinary local variables spent on the release, so many times when we need to use a different variable data, you can use static variables to retain this data
void A () { // ordinary local variables stored in the process stack space, will be released immediately after use // a static local variable compiler will default initialized to 0, stored in the process of global data, released at the end of the program static int A = 1 ; }
(2) modify global variables
Hidden from the other source files
Rule 2: Global variables can cause the same data multiple threads compete with static variables can be used to retain a single source file inside
// global variables persistent memory itself, the static global variable variable for shielding the other source files // global variables are defined in the outer body of the function, storage space is allocated in the global data area, the compiler will automatically and its initialization // ordinary global variables for the entire project visible, other files can be used directly after use extern external declarations, that other documents can not define a variable with the same name // static global variable is visible only to its current file, other files inaccessible, other files can have the same name as a variable // file1.cpp int varA; static int varB; // file2.cpp extern int varA; // use another source of global variables extern int varB; // error, static global variables are shielded
(3) modify global function
Hidden from the other source files
Rule 3:
// non-static function can be directly referenced in another file extern declarations do not add // a static function can only be seen, other documents can not reference the function in a statement on its file // different files can use the same name static function / / file1.cpp extern void FUNA () {} static void funB () {} // file2.cpp extern void FUNA (); // use global variables other source files extern void funB (); // error, static global the function is a function of the shield
III. C ++ code for
Non-static members can access static member, because the life cycle is still there.
Static members can not access non-static member
(1) Modified class static member variables
The default is initialized to 0, keep the memory of long-lasting (be unique, only a memory), this pointer does not exist
// static members can be accessed independently, without having to create any object instance can access // static member variables for each class only one variable memory (global data / static area), while the ordinary member function of each object has different variables memory // test.h class A { private : static const int A; } // test.cpp const int A :: A = . 1 ; // not private and protected access restrictions
(2) Modified class static member function
Keep the memory of long-lasting (be unique, only a memory)
// non-static member functions and variables of the class static member function can not call class, because this is not a static member function pointer // static member functions and variables non-static member function can be called class // static member function can not be at the same time virtual const volatile function declared class A { public : Virtual static void fun1 (); // error static void fun2 () const ; // error static void fun2 () volatile ; // error };