Define, inline, defineinline
# Differences between define and inline
Define:Define macros processed during pre-Compilation;
Only simple character replacement, no type detection
Typedef:Define the type alias for processing complex types
Example: typedef int;
Then: A a; // defines a as int.
Inline:The inline function provides suggestions to the compiler. the compiler has the right to reject the macro replacement.
Not necessarily succeeded in submitting an application
StaticI. background
Cause: when the program executes the variable defined in the function to its definition, the compiler allocates space for it on the stack, the space allocated by the function on the stack is released at the end of the function execution. This creates a problem: If you want to save the value of this variable in the function to the next call, how to implement it?
The easiest way to think of is to define a global variable, but defining a global variable has many disadvantages, the most obvious drawback is that the access range of the variable is broken (so that the variables defined in this function are not controlled by this function ). This is also true for static members of the class.
Solution: Therefore, static is introduced in C ++ to modify variables, which can indicate compilation.
The variable is stored in the static storage area of the program. This achieves the goal and keeps the access range of the variable unchanged.
II. Specific functions
Static analysis summary: static always changes the storage form of variables or objects to Static storage, and the connection mode to internal connections. For local variables (internal connections already exist ), it only changes the storage mode. For global variables (which are already in static storage), it only changes the connection type. (1 connection mode: internal connection; 2 storage mode: stored in the static global Storage Area)
Const
I. background
A C ++ has a strict compilation system, which allows many errors of C ++ programs to be found in the compilation phase, thus greatly reducing the error rate, it has also become an outstanding advantage in comparison with C ++.
A common preprocessing command in B C # define VariableName VariableValue can be easily used for value substitution. This value substitution has at least three advantages:
The first is to avoid the appearance of numbers with vague meanings, so that the program semantics is fluent and clear, as shown in the following example:
# Define USER_NUM_MAX 107 avoids the confusion caused by using 107 directly.
Second, you can easily adjust and modify parameters. In the preceding example, when the number of people changes from 107 to 201, you can modify the parameters here;
Third, it improves the execution efficiency of the program. Because the pre-compiler is used for value substitution, it does not need to allocate storage space for these constants, so the execution efficiency is high. However, the pre-processing statement has many advantages, but it has a fatal disadvantage, that is, the pre-processing statement is only a simple value replacement, and lacks a type detection mechanism. In this way, the pre-processing statement cannot enjoy the advantage of strict C ++ type checks, which may cause a series of errors.
The initial purpose of Const is to replace precompiled commands, eliminate its shortcomings, and inherit its advantages. Now its form is changed:
Const DataType VariableName = VariableValue;
2) specific functions
1. const is used for Pointer Analysis in two cases:
Int const * A; // A variable, * A variable
Int * const A; // A is not variable, * A is variable
Analysis: const is a type modifier that is left-bound. It is the same as its type Modifier on the left.
Type modifier. Therefore, int const is limited to * A, not. Int * const limits A, not *.
2. const limits the passing value parameters of the function:
Void Fun (const int Var );
Analysis: The preceding statement specifies that parameters cannot be changed in the function body.
3. const limits the return value of the function:
Const int Fun1 ();
Const MyClass Fun2 ();
Analysis: The preceding statement limits that the return value of a function cannot be updated. When the function returns an internal type (for example, Fun1), it is already a value. Of course, it cannot be assigned an update, at this time, const is meaningless. It is best to remove it to avoid confusion. When the function returns a custom type (such as Fun2), this type still contains variable members that can be assigned values, so it makes sense at this time.
4. Transfer and return address: This is the most common case. It can be seen from the characteristics of address variables that the appropriate use of const is significant.
5. const qualified class member functions:
Class ClassName {
Public:
Int Fun () const;
.....
}
Note: This post-const form is a provision to avoid confusion. Use const in the declaration and definition of this function, because const has become part of the type information.
Ability to obtain: constant objects can be operated.
Capacity Loss: you cannot modify the data members of a class or call other functions that are not const in a function.
Inline
1) Background
The reason for inline is very similar to const. The inline keyword is used to define an inline function of a class. The main reason for introducing it is to use it to replace C.
Macro definition in expression form.
An example of macro definition in expression form:
# Define ExpressionName (Var1, Var2) (Var1 + Var2) * (Var1-Var2)
This form of expression is similar to that of a function, but it uses a pre-compiler without a stack and is more efficient than a function. However, it is only a simple replacement of the symbol table on the pre-compiler, and does not support parameter validity detection and access control by C ++ class members.
Inline's purposeTo replace the macro definition of this expression form, it eliminates its shortcomings and inherits its advantages. Inline code is efficient in the pre-compiler symbol table. It is a real function and has strict parameter detection when called. It can also be used as a member function of the class.
2) specific functions
Define function members directly in the class definition, and the system processes them as inline functions. member functions are inline functions, meaning that each object has an independent copy of the function.
Outside the class, if the keyword inline is used to define function members, the system will also process them as inline functions;
The difference between an inline function and a macro is that a macro is replaced by a Preprocessor, while an inline function is implemented through compiler control. In addition, inline functions are real functions, but they are expanded like macros when needed. Therefore, the parameter pressure stack of the function is removed, reducing the call overhead. You can call inline functions like calling functions without worrying about macro processing issues. We can use Inline to define Inline functions. However, any function defined in the description section of the class will be automatically considered as an Inline function.
Next we will introduce the usage of inline functions.
Inline functions are valid only when they are declared together with the function body. Declarations such as Inline Tablefunction (int I) have no effect. The Compiler just uses the function as a general function declaration. We must define the function body.
Inline tablefunction (int I) {return I * I };
This defines an inline function. We can call it as a common function. However, the execution speed is indeed faster than that of common functions.
We can also define external functions defined in the class as inline functions, such:
Class TableClass {
Private:
Int I, j;
Public:
Int add () {return I + j ;};
Inline int dec () {return I-j ;}
Int GetNum ();
}
Inline int tableclass: GetNum (){
Return I;
}
All the three functions stated above are inline functions. In C ++, the function of the function body is defined inside the class and is considered as an inline function by default. Whether or not you have the inline keyword.
How can I tell the compiler to make a member function an inline function?
Declaring an inline member function looks very similar to a common function:
Class Fred {public:
Void f (int I, char c );};
But when you define an inline member function, add the inline keyword before the member function definition and put the definition in the header file: inlinevoid Fred: f (int I, char c) {//...} usually, the function definition ({...} in the header file. If you place the inline function definition in the. cpp file and call it in other. cpp files, the connector will give an "unresolved external" error.
Inline functions are the most widely used in C ++ classes and should be used to define access functions. Generally, data members are defined as private or protected in the classes we define. In this way, the outside world cannot directly read and write the data of our class members. To read and write private or protected members, you must use the member interface function. If we define these read/write member functions as inline functions, the efficiency will be better.
Class sample {
Private:
Int nTest;
Public:
Int readtest () {return nTest ;}
Void settest (int I) {nTest = I ;}
}
Of course, inline functions also have some limitations. That is, the Execution Code in the function cannot be too much. If the function body of the inline function is too large, the general compiler will discard the inline method and call the function in the normal way. In this way, the efficiency of inline functions is the same as that of normal functions.
C keyword
# Define macro name code to be replaced
Macro definition, which is saved in the pre-compiler symbol table for efficient execution. It is used as a simple symbol replacement and does not check the parameter validity.
New Type of existing type of typedef
Aliases are often used to create platform-independent types. typedef is interpreted during compilation, So it allows the compiler to replace text beyond the pre-processor capability.