1. Global static variables in the cpp File
Global declaration:
Static int a = 0;
Such static global variables can only be used by the cpp file and cannot be shared by other cpp files.
If static is not declared:
Int a = 0;
All such variables cannot guarantee that a can be shared by other cpp files, nor can it be shared by other cpp files. It is not recommended to use this method. It is best to add extern:
Extern int a = 0;
2. Static local variables
Int f ()
{
Static int a = 1;
}
Static local variables are stored in the static variable area, rather than in the stack. Therefore, the life cycle is the entire program cycle, not the function cycle. Apply for memory only once, and then save the value of last a when the function is called next time.
3. Global static functions in the cpp File
Declare in cpp:
Static int func ();
Like Global static variables in cpp, the function only applies to the cpp file. If you want to call multiple cpp files, put them in the header file and do not add static.
4. Static members of a class
Class
{
Private:
Static int;
}
The static member scope is Class A, and variable a is A shared member variable of Class A, instead of all of an instance. Static member a must be initialized in cpp:
Int A: a = 0; // Note: There is no static modifier here, because static is A declarative keyword.
5. static member functions in the class
Class
{
Private:
Static void func (int );
}
When this function is implemented, the keyword static is not required because static is a declarative keyword;
The static member function of A class is A global function in the category of this class. It cannot access private members of this class. It can be called without an instance. :: func (9 );
Static member functions can be inherited and overwritten, but cannot be virtual functions.
Static does not have the this pointer.
Summary: in fact, the static principle is that it is in the static variable area of the memory, so the life cycle is different. Second, he applies only once, so it will not be overwritten.