Chapter 09 Name Control
First, static members from C language
In C and C , Static has two basic meanings, and these two meanings are often conflicted: 1) assignment on a fixed address, that is, the object is in a special static data area. Created on the stack, not when the function is called. This is also the concept of static storage. 2) It is local to a specific compilation unit (just like we will see later, this includes the scope of the class in C ). Here STATIC controls the visibility of the name, so this name is invisible outside this unit or class. This also describes the concept of connection, which determines which names will be seen.
Second, the destructor of static objects
The destructor of the static object (including all objects of static storage, not just partial static variables in the previous example), or when the program is exited from the main () block, or the standard C library function exit () is called transfer. In most cases, the end of the main () function is also called EXIT () to end the program. This means that using exit () is very dangerous in the destructor, as this may fall into a dead cycle. However, if you use a standard C library function Abort () to exit the program, the destructive function of the static object will not be called.
Third, control connection
In general, all the names (neither nested in class or names) within the file are visible to all compilers in the program. This is the so-called external connection because this name is visible to the connector at the time of connection, and the external compiler, global variables, and normal functions have external connections. One advantage of the internal connection is that this name can be placed in a header file without worrying about the connection.
Those names, like a constant, inline function (inline function), in the default (of course, only in C default, in) C The default is an external connection). Note that the connection only references those members with address during connection / loading, so the class declarations and local variables are not connected.
Fourth, static member functions
Like a static data member, we can also create a static member function, which is a class all of the service instead of a part of the object service. This doesn't need to define a full-class function, reduce the occupation of global or local names space, move this function to the inside of the class. When a static member function is generated, a connection with a particular class is also expressed. Static member functions cannot access a general data member, which can only access static data members, and can only call other static member functions. Typically, the address of the current object (this) is a function that is implicitly passed to the called being called. But a static member function does not have this, so it is unable to access the general member function. This uses a static member function to have a little growth in the speed than the global function, which not only does not deliver the additional cost required for this, but also benefits the function in the class. Because of the initialization method of the static member object, we can put a static data member of the above class in the inside of the class.
Five, static initialization dependence factors
There are three ways to handle this problem: 1) Don't use it to avoid interdenectation. This is the best solution. 2) If you need to use, put the definitions of the key static objects in a file so that we can guarantee their correct initialization as soon as they allow them in the file. 3) If we are confident that the static object is placed in several compilation units is inevitable (when writing a library, we cannot control programmers who use the library) At this time we can use Jerry Schwarz to create an Iostream library ( A technique provided when CIN, COUT and CERR is provided in different files). This technology requires an additional class in the library header file. This class is responsible for the dynamic initialization of static objects in the library. (Similar to Singleton) 6, conversion connection designation
What should I do if I write a program in C to use a C library? If this declares a C function: float f (int A, char b); C compiler will turn this name to something like _f_int_int to support function overload (and type security connection). However, the library compiled by the C compiler generally does not do such a conversion, so its internal name is _f. In this way, the connector will not solve our C to F () calls. A connection conversion is provided in C , which is implemented by overloading the EXTERN keyword. After both, you will specify a string to specify the connection type of the function we want, followed by the function declaration. EXTERN "C" float f (int A, char b);
This tells the compiler f () to be C connected, so that the function name will not be converted. Standard connection type specifiers have both "C" and "C ", but the compiler developer can choose to support other languages in the same way. If we have a set of conversions, you can put them in the curly brackets: extern "c" {float f (int a); float g (int b);}