Inline functions)
Restrained functions are generally relatively small in size. When the program calls these member functions again, they are not really calling the function (such as saving the return address ), instead, the function code is embedded into the call point of the program (the restrained function is not called in the source file but expanded as is), which can greatly reduce the call time of the member function.
The member functions defined in the class are restrained by default. In C ++, class definitions are generally placed in header files, so the functions defined in these classes are also written into header files. Function declaration is generally in the header file, and definition is not allowed in the header file, because they need to be compiled multiple times. If it is an restrained function, it is allowed to be written in the header file.
Restrained function definition:
Inline functions are valid only when they are declared together with the function body. The Inline tablefunction (int I); statement is ineffective. The Compiler just uses the function as a normal function declaration. We must define the function body.
Inline tablefunction (int I) {return I * I };
Application of restrained functions:
Application 1: define access functions for class member variables
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;} // read the member variable
Void settest (int I) {ntest = I;} // sets the member variable
}
Application 2: implement macro effects
When a macro is used, no parameter check is performed, which is prone to ambiguity. For example, to calculate x square:
# Define table_multi (x) * (x ))
If the input parameter is expression a ++; (a = 4), they want to get the result (a + 1) * (a + 1). What is the actual result? Let's take a look at the macro expansion result: (A ++) * (a ++). If the value of A is 4, the result is 5*6 = 30.
This is not the case if you change the function to an restrained function, because the restrained function is a real function and the function is called.
Inline int square (int x) {return x * X ;};
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.
1. Features of macros
Why use Macros? Because the function call must transfer the execution sequence of the program to an address stored in the memory of the function. After the program content of the function is executed, return to the place before the function is executed. This transfer operation requires that the on-site address should be stored and remembered before the transfer is executed. After the transfer, the on-site address should be restored and the operation should be continued based on the original storage address. Therefore, function calling requires a certain amount of time and space overhead, which affects its efficiency. Macros only expand the code in the pre-processing area without additional space and time overhead. Therefore, calling a macro is more efficient than calling a function.
Macro has many unsatisfactory aspects.
1) macros cannot access private members of objects.
2) The definition of macros is prone to ambiguity.
For example: # define table_multi (x) * (x ))
Table_multi (A ++) calls it. They intend to get (a + 1) * (a + 1) results. What is actually? Let's take a look at the macro expansion result: (A ++) * (a ++). If the value of A is 4, the result is 5*6 = 30.
The following describes how to solve these problems using the inline functions I want to introduce. We can use inline functions to replace macro definitions. In fact, we can replace the pre-processing macro With 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.
Differences between macro definitions of restrained functions