2) Global static variables are global variables explicitly modified with static. The scope is the file where the variable is declared. Other files cannot be declared using extern.
-Static local variables#include
void fn();void main(){ fn(); fn(); fn();}void fn(){ static int n=10; printf("%d",n); n++;}
Static local variables solve the problem: a variable is defined in the function body, and stack memory is allocated to the local variable whenever the program runs the statement. However, as the program exits from the function body, the system will reclaim the stack memory and the local variables will also become invalid. Or you need to save the variable value between two calls.
Some people may say, why don't we get cooler globally? If a global variable is used, the variable no longer belongs to the function itself, and is no longer only controlled by the function, causing inconvenience to program maintenance.
Static local variables can solve this problem. Static local variables are stored in the global data zone, instead of in the stack. Each value is kept to the next call until the next value is assigned.
-Static Functions# Include
Static void fn (); // declare the static function void main () {fn ();} void fn () // define the static function {intn = 10; printf ("% d", n );}
The difference between a static function and a common function is that it can only be seen in the file where it is declared and cannot be used by other files. Functions with the same name can be defined in other files without conflict;
Static
-Static data memberAdd the keyword static before the declaration of the data member in the class. The data member is the static data member in the class.
# Include
ClassMyclass {public: Myclass (inta, intb, intc); void GetSum (); private: int a, B, c; static int Sum; // declare static data members }; intMyclass: Sum = 0; // defines and initializes the static data member Myclass: Myclass (inta, intb, intc) {this-> a =; this-> B = B; this-> c = c; Sum + = a + B + c;} voidMyclass: GetSum () {cout <"Sum =" <
Feature: no matter how many objects are created in the class, the static member only keeps one copy in the memory. This copy is shared by all objects in the class. That is to say, static data members are shared by all objects in the class. For multiple objects in this class, static data members only allocate memory once for all objects. Therefore, the values of static data members are the same for each object, and their values can be updated;
Static data members follow the public, protected, and private access rules like common data members;
Two essential parts of static members: In-class declaration and out-of-class initialization
Static data member Initialization is different from general data member initialization. The static data member initialization format is:
<; Data type> <; Class Name >:<; static data member name >=<; value>
Static data members of a class can be accessed in two forms:
<; Class Object Name>. <; static data member name> or <; class type name >:<; static data member name>
-Static member functions
Class Myclass {public: Myclass (int a, int B, int c); static void GetSum (); // declare the static member function private: int a, B, c; static int Sum; // declare static data member} int Myclass: Sum = 0; // define and initialize static data member Myclass: Myclass (int a, int B, int c) {this-> a = a; this-> B = B; this-> c = c; Sum + = a + B + c; // non-static member functions can access static data members} void Myclass: GetSum () // static member function implementation {// cout <
Static member functions can be defined in the class. It can also be declared in the class and defined outside the class. When defining a non-class definition, you cannot use the static keyword as the prefix.
Call Method
-Class 1: static member functions
As shown above
Myclass::GetSum();
-2 object. static member function
As shown above
M.GetSum();
Note the following before calling a static member function through an object:
1. The prerequisite for accessing static member functions through objects is that the object has been submitted. 2. The access permission for static member functions is the same as that for common member functions. 3. static member functions can also omit parameters and use the default parameter value, and heavy load.
The difference between static member functions and common functions and the use of member functions: because a static member function has only one copy in the class, the members who access the object need to receive some idle: static member functions can directly access static members described in the callback class, but cannot directly access non-static members in the callback class. to access non-static members, you must obtain the corresponding object by passing parameters, and then access it through an object. Note that static members do not have the this pointer because they are independent of objects.