Small static

zhaozj2021-02-16  61

This is an article written before, huh, huh, iron comes out for reference only

1, what is static? Static is a very common modifier in C , which is used to control the storage methods and visibility of variables.

2, why should I introduce a variable defined in the Static? Function, when the program executes it to its definition office, the compiler allocates space on the stack. Let's know that the function assigned on the stack is allocated at this function. Released, this has a problem: how can you implement if you want to save the value of this variable in the function to the next call? The most easy way to define a global variable, but there are many shortcomings defined as a global variable, the most obvious disadvantage is to destroy the access range of this variable (so that variables defined in this function are not only controlled by this function ).

3. When is Static? It takes a data object to be the entire class rather than an object service, and it is necessary to do not destroy classes, that is, the member is required to hide the inside of the class and is not visible.

4, STATIC's internal mechanism: The static data member must exist when the program is running. Since the function is called in the program run, the static data member cannot allocate space and initialization within any function. In this way, its spatial distribution has three possible places, one is the header file as an external interface, there is a class declaration; the second is the internal implementation of the class definition, there is a class of member function definitions; three is the application of the application () Global Data Declaration and Definition before the function. Static data members must actually allocate space, so they cannot be defined in the category declaration (only data members can be declared). Class declarations only declare a class of "size and specifications", do not perform actual memory allocation, so write a definition in class declaration is wrong. It cannot also be defined in the external definition of category in the header file, because it will be repeatedly defined in multiple source files that use this class. Static is introduced to inform the compiler, store the variable in the static storage area of ​​the program, and the static data member is initialized in order, pay attention to the static member nested, and to ensure that the nested members have been Initialized. The order of elimination is the reverse order of initialization.

5, Static's advantage: Save memory because it is public, so for multiple objects, static data members are only stored for all objects. The value of the static data member is the same, but its value is updated. As long as the value of the static data member is updated once, all objects have been updated after the update, which can improve time efficiency.

6. When references a static data member, use the following format: :: If the access permission of the static data member is allowed (ie public member), you can reference static in the program. Data members.

7. Note: (1) The static member function of the class is an object belonging to the entire class, so it doesn't have this pointer, which results in only the static data and static member functions of the class. (2) Can't define the static member function as a virtual function. (3) Since the static member is declared in the class, it is operated outside, so however, how many special, the variable address is a pointer to its data type, the function address type is a "Nonmember Function Pointer".

转载请注明原文地址:https://www.9cbs.com/read-23860.html

New Post(0)