1. Static Global variables
By adding the static keyword between global variables, it is defined as a static global variable.
Features: allocating memory in the global data area
Uninitialized is automatically initialized to 0
Visible within the file that declares it, files outside the declaration are not visible, and other files can use the same name variable
2. Static local Variables
Add the static keyword to the local variable, which is defined as a static local variable
Features: allocating memory in the global data area, but scoped to local scope
It is typically initialized at declaration time and can only be initialized once, and is not initialized when the function is called at a later time.
If initialization is not displayed, initialize to 0
3. Static functions
With the static keyword before the function return type, it is defined as a static function
Features: Visible only in the file that declares it, not in other files
Functions with the same name can be found in other files
4. Static data members
The static keyword is defined before the class data member declaration, and is
Features: Regardless of the number of class objects, static data members have only one copy, shared access by all objects, only one memory allocated once, the value can be updated
Static data members are stored in the global data area and are allocated space when defined, so they cannot be defined at declaration time (Assignment) and can be initialized when the first object is declared, in the form
The data type >< class name >::< the static data member name >=< value, which can be placed in the class definition so that the class declares the object and initializes
Static data members adhere to Public,private,protect access rules
Information hiding is possible compared to global variables, and there is no naming conflict because it does not enter the global namespace
5. Static member functions
Adding the Static keyword before the class member function is defined as a static member function
Features: Service for a class instead of an object, is an internal implementation of the class and is part of the class definition
Because the static member function does not contact the class object, there is no default this pointer, so non-static data members cannot be accessed, and non-static member functions cannot be accessed
In addition to using the member access operator (. and), you can also access static member functions directly, in the form of the class name >::< static member function name (parameter table)
C + + Static keyword Summary