C ++ Differential Series Talk (1)

zhaozj2021-02-11  192

C Differential Series Talk (1)

Zheng Liqun

Static is a very common modifier in C , which is used to control the storage methods and visibility of the variable. Here I will talk about the fact that the STATIC modifier is fully analyzed from the Static modifier. Two major roles of Static: 1. Control storage mode: Static is introduced to inform the compiler, store the variable in the static storage area of ​​the program and not stack space. 1. Terminal: The variable defined inside the function is executed to its definition at the program, the compiler allocates space on the stack. Let's know that the space allocated on the stack is released at the end of this function. This creates a problem: How can I achieve if I 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 ). 2, Solution: STATIC is introduced in C , use it to modify the variable, it can indicate that the compiler saves this variable in the static storage area of ​​the program, so that the purpose is achieved, and the access range of this variable constant. Second, Control Visibility and Connection Type: Static has a role, which will limit the visible range of the variable to the compilation unit, making it an internal connection, at this time, its antonym is "extern". Summary : STATIC always makes the variable or object storage form into static storage, the connection mode becomes internal connection, for local variables (already internal connection), it only changes its storage; for global variables (already static storage) ), It only changes its connection type. STATIC members in the class: 1. Reasons and effects: 1, need to interact between individual objects of a class, that is, a data object is required for the entire class rather than an object service. 2. At the same time, it is necessary to do not destroy the encapsulation, that is, this member is required to hide the interior of the class and is not visible to the outside. The STATIC member of the class satisfies the above requirements because it has the following characteristics: there are independent storage area belongs to the entire class. Second, Note: 1. For static data members, the connector guarantees that it has a single external definition. Static data members are initialized in order by definition, pay attention to static members nested, to ensure that the nested members have been initialized. The order of elimination is the reverse order of initialization. 2, the static member function of the class is an object that belongs 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.

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

New Post(0)