C ++ programming ideological notes

zhaozj2021-02-16  52

Chapter 7 constant

First, the value is replaced: When designing the C language, the preprocessor can establish a macro without restrictions and use it to replace the value. Because the preparation is only a text alternative, it has no type checking idea, and there is no type inspection tool, so the pre-processor's value will produce some small problems, which can be avoided in C by using const. The Const default in C is internal connection, that is, Const is visible only in Const only, and cannot be seen by other compilers when connected. When defining a constant const, a value must be assigned, unless clear descriptions in Extern const buffs; C compiler is not all assigned storage space for Const, but in turn save this definition in it Symbol table. When Const is used, it performs constant folding when compiling.

Two cases of allocating memory: 1, the above EXTERN enforces storage space allocation (the constant of the internal connection does not assign an address, and the constant of the external connection requires an address) 2, and there are some cases The address of the const is also possible to make a storage space allocation. For internal data types used in a large number of applications, including constant expressions, compilers can perform constant folding. Of course, it is impossible for any consT allocation storage, especially for complex structures. In this case, the compiler establishes storage, which prevents constant folding. This (sometimes assigned memory to constant) is why constant must be connected within the default, that is, the connection is only within the special compilation unit; otherwise, due to the allocation of many constings, it is easy to cause connection errors, connection programs. I saw the same definition in multiple object files will "complain" (found the same name). However, because const defaults to internal connections, the connection program does not across the compiler, so there is no conflict. (C default const is external connection)

When processed a const pointer, the compiler will still work hard to prevent storage and perform constant folding. We can assign a non-Const object to a const pointer because it may sometimes want to change some things that can change. However, the address of a const object cannot be assigned to a non-Const pointer, as this may change this const pointer by assigning pointers. Of course, this can always be used to enforce such assignments (INT * a = (int *) & e forced address), but this is not a good program habit, because this breaks the object's const attribute and by const. Provided security.

Function parameters and return values: Use constly defined function parameters and return values ​​to the constant concept. Another easy place is easy to confuse. If the user (the caller of the function) is transmitted in terms of value, it is meaningless (it means that the parameters passing in the function cannot be modified), but the designer of the function is There must be a certain role to ensure that a parameter will not change. So it is actually a function of a function creation, not a function of a function caller. In order not to confuse the caller, use the const qualified parameter in the function inside the function to define the parameters in the parameter table. This means that the return value cannot be modified by the constant to return the value of the user-defined type, which means that the return value cannot be modified. If the address is passed and returned, Const will guarantee that the address content will not be changed. For internal data type INT, FLOAT, CHAR, etc. The programmer is not confused. When processing the type of user-defined, the return value is constant. If a function returns a value of a class object, its value is constant, then the return value of this function cannot be a left value (ie it can't be assigned, and cannot be modified). When returning a value of an internal data type, Const meaningless is that the compiler does not allow it to become a left value (because it is always a value rather than a variable). The above problem occurs only when returning the value of the user-defined type object. If you pass or return a pointer (or a reference), the user takes a pointer and modifies the initial value. This is very important if this pointer is constant a constant pointer. In fact, whenever you pass an address to a function, we should use const to modify it as much as possible. If you don't do it, make the pointer function that pointing to Const does not have usability. Do you choose to return a pointer to the constant, depending on what we want to use it. Due to the reason why the referenced syntax (like the value is transmitted), it is possible to pass a temporary object to a referenced function, but cannot Pass a temporary object to a function with a pointer - because it must be clearly provided. Therefore, by reference delivery will generate a new situation that does not appear in C: A temporary variable that is always constant, and its address can be passed to a function. This is why the temporary variable is passed to a function by reference to a function, and the parameter of this function must be a constant reference. It is especially important to use constant references in function parameters. This is because our function may accept temporary objects, which is explicitly created by the return value of another function or explicitly created by the function user. The temporary object is always constant, so if the constant reference is not used, the parameters will not be accepted by the compiler. f (1)

Second, the collection: Const can be used for collection (arrays) const Int a = {1, 2, 3, 4}, but the compiler cannot store a collection in its symbol table, so you must allocate memory. In this case, Const means "a piece of storage that cannot be changed". However, its values ​​cannot be used when compiling, because the compiler does not need to know the store when compile, only when it is running (although it has already determined its value, but do not know its storage location)!

Third, CONST and ENUM in the class

At first readers may think that the logical choice is to put a const in the class. But this does not produce expected results. In a class, Const restores part of its meaning in C. It assigns a store in each class object and represents a value, and this value cannot be changed once it is initialized. In a class, use constant "In this object life, this is a constant". However, for this constant, each different object can contain a different value. When establishing a const in a class, you cannot give it an initial value. This initialization must occur in the constructor, and to a particular place to construct a function. Because const must be initialized in the construction of it, the const must be initialized in the main body of the constructor, otherwise, only waiting until some place after the constructor is initialized, this means for a while Initialize Const. Fourth, const objects and member functions

The compiler emphasizes the object to const, so it must ensure that the object's data is not changed during the object life. It can easily guarantee that public data is not changed, but how do you know which member function changes data? How do you know which member function is "safe" for the const object? If a member function is declared for a const function, it is equal to telling the compiler to call this function for a const object. A member function that is not specifically declared as const is a function that will modify the data member in the object, and the compiler does not allow this function to call a const object. . Just declare that a function is const in the class definition, and cannot guarantee that the member function is also defined, so the compiler forces the programmer to reiterate the Const description when defining a function. (Const has become part of a function identifier, so both compilers and connectors are checked.

VOLATILE

Volatile's syntax is the same as const, but Volatile means that this data can be changed in the range of compilers. I don't know what, the environment is changing data (possibly through multi-tasking), so Volatile tells the compiler to do any assumptions about data - especially important during optimization. If the compiler says: "I have read the data into the register, and no longer contact with registers." In general, it does not need to read this data. However, if the data is volatile modified, the compiler cannot make such assumptions, as it may be changed by other processes, it must reread this data rather than optimizing this code.

Chapter 8 Inline Functions

First, inline function and compiler

In order to understand when inline is effective, you should first understand what the compiler encounters an inline function. For any function, the compiler places the function type in its symbol table (ie, the number of function prototypes and functions of the name and parameter type). In addition, the compiler sees that the inline function and the inline function are not erroneous, and the code of the function is also placed in the symbol table. The code is stored in the form of a source program or is stored in the form of compiler depends on the compiler. When calling an inline function, the compiler first ensures that the call is correct, that is, all parameter types must be the correct type or the compiler must be able to convert the type to the correct type, and the return value should be the correct type or change in the target expression. To the correct type. Of course, the compiler does do this for any type of function, which is significantly different from the preprocessor because the preprocessor cannot check the type and conversion. If all the function type information is in line with the context called, the inline function code will replace the function call directly to eliminate the overhead of the call. If the inline function is also a member function, the address of the object (this) will be placed in a suitable place, which is of course the preprocessor cannot be executed.

Second, the logo paste

The logo paste is very useful when writing code. It allows us to set up two identifiers and paste them together to automatically generate a new identifier. #define field (a) char * a ## _ stringmain () {field (a); a_string = "aaaa"; cout << a_string << endl;}

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

New Post(0)