Before describing the internal function, it is necessary to introduce the pretreatment macro. The function of the inline function is similar to the function of preprocessing macros.
. I believe everyone has used the pre-dealing macro, we will often define some macros, such as
#define table_comp (x) ((x)> 0? (x): 0)
Define a macro.
Why use macro? Because the call to the function must transfer the order of the program to the function stored in the function
A address in the memory, after executing the program content of the function, returns to the place where the function is executed.
This transfer operation requires saving the site and remembers the execution of the address before turning, and then restores the site, and
Press to save the address to continue execution. Therefore, the function call has a certain time and space overhead, so
Affects its efficiency. And the macro is just in the pre-processed place to unfold the code, no additional space and time
Pin, so call a macro to call a function more efficient.
But there are many things that are not satisfactory.
1. The macro cannot access the private member of the object.
2, the definition of macros is very easy to generate birman.
We will give an example:
#define Table_Multi (x) (x * x)
We call it with a number, Table_Multi (10), which looks no mistake, the result is returned
100, is correct, but if we use Table_Multi (10 10) to call, the result we expect is 4
00, while the result of the macro is (10 10 * 10 10), the result is 120, which is obviously not the result we have to get. avoid
For these erroneous methods, one is to give macro parameters plus parentheses.
#define Table_Multi (x) ((x) * (x))
This ensures that it will not be wrong, but even if this definition is used, this macro is likely to be wrong.
If you use Table_Multi ( A) to call it, they intend to get the result of (A 1) * (A 1), but actually
? We can look at the results of the macro: ( a) * ( a), if a value is 4, the result we get is 6 * 6 =
36. The result we expect is 5 * 5 = 25, which has emerged. In fact, there is this in some C library functions.
Some questions. For example: TouPper (* pchar ) will perform two operations for PCHAR, because TouPper actually
It is a macro.
We can see that there are some difficult problems, how to solve it?
Here is to solve these problems with the inline function I want to introduce, we can use the inline function to replace the macro
Definition. And in fact, we can completely replace the pretreatment macro with the inner function.
The difference between the inline function and the macro is that the macro is replaced by the preprocessor, and the inline function is compiled.
User control is implemented. And the inline function is a real function, just when needed, the inline function image macro
The same start, so the parameter stack of the function is canceled, which reduces the overhead of the call. You can like a function like a function
To call the inline function, don't worry that it will be generated in some problems with the macro.
We can define inline functions with inline, but any functions defined in the classes are subject to
Automatic think is the inline function.
Let's introduce the usage of inline functions.
The inline function must be made with a function of the function. Apply in this article inline Tablefuncti
ON (INT i) is no effect, the compiler just declares the function as a normal function, we must define a function
.
Inline Tablefunction (INT I) {Return i * i};
This way we can't define an inline function. We can call it as a general function. but
Performing speed is indeed faster than the execution speed of the general function.
We can also define the function of the exterior of the class as an inline function, such as:
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;
}
The three functions declared above are inline functions. In C , the function of the function body is defined within the category,
The default is the inline function. No matter if you have inline keywords.
Inline functions are in the C class, the most widely used, should be used to define access functions. Our defined class
Will define data members into private or protected, so that the outside world cannot directly read the data of our members.
. For the read and write to the private or protective members, the member interface function must be performed. If we read these
Write a member function that is defined as an inline function, it will get a better efficiency.
Class Sample {
Private:
Int ntest;
PUBLIC:
INT readtest () {return ntest;}
Void Settest (INT I) {ntest = i;}
}
Of course, the inline function also has certain limitations. Is the execution code in the function can't be too much, if, within
The function of the function is too large, and the general compiler will abandon the inline manner, and the function is used to call the function. This
The inline function is the same as the peripheral execution efficiency.