C ++ Programming Thoughts Reading Notes - Static

xiaoxiao2021-03-06  14

Static keyword

Static storage, allocated on a fixed address, that is, the object is created on a special static data area instead of each time the stack is generated at the function call.

The static object inside the function is that if the object is a class, the program controls the first time to transfer to the object definition, and only the first time, the constructor needs to be executed. The destructor of the statically object destructor static object is called when the program exits from the main () block, or the standard C library function exit () is called. If a function containing static objects has never been called, the constructor of this object will not be executed, and the destructor will not be executed. In C , the constructor of the global static object is called before main (), so we now have a simple, portable approach to execute a code before entering main (), and can exit main () Execute the code with the analysis function. To do this in C, we have to be familiar with the start code of the compiler developer's assembly language. It is local for a specific compilation unit. Here STATIC controls the visibility of the name, so this name is invisible outside this unit or class.

In general, all the names in the file range (nen nested in class or names) are visible to all compilers in the program, which is the so-called external connection. Within the file range, a local variable is made to the compilation unit (generally referring to a .cpp file) of the object or function of Static, which is a so-called internal connection. The advantage of the internal connection is that this name is placed in a header file without worrying about the connection. Those names, quantities, inline functions, default (C are internal connection) conflicts, how are the internal connection, and c is an external connection.

All global objects are hidden as static storage classes. INT A = 0; main () {/*...*/}, a static data area stored in the program, before entering the main () function, A is initialized. In addition, A is visible to the global, including all compilation units. With visibility terminology, Static (visible in compilation unit) is extern, which means that this name is visible to all compilation units. INT A = 0; Extern Int A = 0; the above two effects are the same. Static int A = 0; this definition only changes A visibility, now A is an internal connection. But the storage type has not changed.

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

New Post(0)