C++ static keyword summary

Static local variables are
located inside the function. They are initialized when the function is called for the first time and are initialized once. Calling the function again will skip the initialization statement of static local variables; static local variables are stored in the static area and will not follow the function. The call ends and is released.
The static global variable
is located outside the function and stored in the static area. The scope is the source file that defines the variable.

Static local variable Static global variables Local variable Global variable
Storage area Static zone Static zone Stack area Static zone
Scope Function body The source file that defines the variable Function body All source files
initialization Initialize once when the function is called Initialize once Initialize every time the function is called Initialize once

Static member variables
1. Static member variables are declared in the class, defined and initialized outside the class. Objects cannot be initialized by calling the constructor. Constant static member variables can be initialized in the class.
2. Static member variables are shared by all objects of the class.
3. It can be accessed either through the class or through the object.
4. It can be used as the default actual parameter.
5. Allocate memory in the static area and do not count into the memory calculation of the class.
6. The static member variable type can be an incomplete type.
Static function
The scope of a static function is the source file that defines the function, and the scope of a non-static function is all source files.
Static member functions
1. Static member functions belong to a class and not to an object.
2. Static member functions cannot access non-static member variables and non-static member functions.
3. The static member function does not contain the this pointer, so it cannot be declared as a const constant function.

Guess you like

Origin blog.csdn.net/Huang_JinXin/article/details/88901185