The role of C++ static keyword

1. Let me introduce its first and most important one: hiding . (Static function, static variable can be used)

When multiple files are compiled at the same time, all global variables and functions that are not prefixed with static have global visibility.

2. The second function of static is to keep the variable content persistent . (Memory function and global lifetime in static variables)

The variables stored in the static data area will be initialized at the beginning of the program, which is the only initialization. There are two kinds of variables stored in the static storage area: global variables and static variables, but compared with global variables, static can control the visible range of variables. After all, static is used to hide. Although this usage is not common if it is defined in a function as a static local variable, its lifetime is the entire source program, but its scope is still the same as an automatic variable, and the variable can only be used in the function in which the variable is defined. After exiting the function, although the variable still exists, it cannot be used.

#include <stdio.h>
 
int fun(){
    static int count = 10; //在第一次进入这个函数的时候,变量a被初始化为10!并接着自减1,以后每次进入该函数,a
    return count--; //就不会被再次初始化了,仅进行自减1的操作;在static发明前,要达到同样的功能,则只能使用全局变量:    
 
}
 
int count = 1;
 
int main(void)
{
     printf("global\t\tlocal static\n");
     for(; count <= 10; ++count)
               printf("%d\t\t%d\n", count, fun());
     return 0;
}

3. The third function of static is to initialize to 0 by default (static variable)

In fact, global variables also have this attribute, because global variables are also stored in the static data area.

4. The fourth role of static: class member declaration static in C++

 When a static variable or function is declared in a class, the scope operator is used to indicate the class it belongs to during initialization. Therefore, static data members are members of the class, not members of the object, so that the following effects occur:

(1) The static member function of a class is an object belonging to the entire class rather than the object of the class, so it has no this pointer, which causes it to only access the static data and static member functions of the class.      

(2) Static member functions cannot be defined as virtual functions.      

(3) Since static members are declared in the class and operate outside of it, the addressing operation is somewhat special. The variable address is a pointer to its data type, and the function address type is a "nonmember function pointer".

(4) Since the static member function does not have this pointer, it is almost equivalent to the nonmember function. As a result, it has an unexpected benefit: becoming a callback function allows us to combine C++ and C-based XW indow systems, and at the same time Successfully applied to thread functions. (I haven't met this one)  

(5) Static does not increase the time and space overhead of the program. On the contrary, it also shortens the access time of the subclass to the static members of the parent class and saves the memory space of the subclass.      

(6) For static data members, add the keyword static in front of <definition or description>.      

(7) The static data member is statically stored, so it must be initialized. (The programmer manually initializes, otherwise it will not report an error when compiling, but it will report an error when linking) 

(8) Static member initialization is different from general data member initialization:

Initialization is carried out outside the class, and static is not added in front to avoid confusion with general static variables or objects;
the member's access control symbols private, public, etc. are not added during
initialization ; the         scope operator is used to indicate the class it belongs to during initialization. ;
           So we get the format of static data member initialization:
<data type><class name>::<static data member name>=<value>

(9) In order to prevent the influence of the parent class, you can define a static variable in the child class that is the same as the parent class to shield the influence of the parent class. One thing to note here: We say that static members are shared by the parent class and the subclass, but we have repeatedly defined static members. Will this cause an error? No, our compiler uses a wonderful technique: name-mangling is used to generate a unique logo.

Guess you like

Origin blog.csdn.net/qq_35789421/article/details/113603115