C ++ Inline Function (Inline) Introduction (Transfer)

zhaozj2021-02-16  77

-------------------------------------------------- -------------------------------- November 13, 2001   Source: Tianji Net   瑾 瑾 Introduce Inline Functions Before, it is necessary to introduce the pretreatment macro. The function of the inline function is similar to the pre-process macro. I believe everyone has used the pre-processed macro, we will often define some macros, such as #define table_comp (x) ((x)> 0? (X): 0) defines a macro. Why use macro? Because the call to the function must transfer the order of the program to a function stored in the memory, after the function's program content is executed, return to the place before 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 keeps the address according to the original save address. Therefore, the function call has a certain amount of time and space, which will affect its efficiency. And the macro is only unfolded in the pre-processed place, does not require additional space and time overhead, so call a macro to call a function more efficient. But there are many things that are not satisfactory. 1,. Macros cannot access private members of the object. 2,. The definition of macros is very easy to produce bidding. We will take an example: #define table_multi (x) (x * x) We use a number to call it, table_multi (10), which seems to have no mistake, the result is returned 100, it is correct, but if we use Table_Multi ( 10 10) If we want to call, the result we expect is 400, while the result of the macro is (10 10 * 10 10), the result is 120, which is obviously not what we have to get. Avoiding these mistakes, 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 can make mistakes, such as using Table_Multi (A ) to call it, they are interested I hope to get (A 1) * (A 1) results, but actually? We can look at the outflow of macros: (A ) * (A ), if the value of A is 4, the result we get is 5 * 6 = 30. The result we expect is 5 * 5 = 25, which has emerged. In fact, there are also these problems in some C library functions. For example, TouPper (* PCHAR ) performs two operations for PCHAR, because TouPper is actually a macro. We can see that there are some difficult problems, how to solve it? Here is to solve these problems with the inline functions 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 macro is that the macro is replaced by the preprocessor to the macro, and the inline function is implemented by compiler control. Moreover, the inline function is a true function, but when it is needed, the inline function is the same as the macro, so the parameter stack of the function is canceled, reducing the overhead of the call. You can call the inline function like a function of calling the function, but don't worry that some of the issues that will be generated in handling macros. We can define inline functions with inline, but any functions defined in class instructions are automatically considered to be inlined functions. Let's introduce the usage of inline functions. The inline function must be made with a function of the function.

The inline TableFunction (INT i) is therefore no effect, the compiler is just a function of the function as a common function, and we must define a function. Inline TableFunction (INT I) {Return I * i}; so that we count for an inline function. We can call it as a general function. However, the execution speed is indeed faster than the execution speed of the general function. We can also define the function defined outside the class as an inline function, such as Class TableClass {Private: INT I, J; Public: int Add () {Return i J;}; inline int dec () {Return IJ;} int getnum ();} inline int TableClass :: getnum () {return i;} The three functions of the above declaration are inline functions. In C , the function of the function body is defined in the inside of the class, which is default that 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. In our defined classes, the data will generally define 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 define these read-and-write member functions into inline functions, it will get a better efficiency. Class sample {private: int} void settest (INT i) {ntest = i;}} Of course, the inline function also has certain limitations. It is not too much execution code in the function. If the function of the inline function is too large, the general compiler will discard the inline manner, and the function is used in a normal way. In this way, the inline function is the same as the normal function execution efficiency. In fact, it is very simple, it is the keyword of the inline function. If a function is defined as an inline function, when the program is compiled, the code of the inline function is inserted directly into the location where the function is called, this approach saves the runtime running time.

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

New Post(0)