Introduction to C ++ inline functions (Inline)

Source: Internet
Author: User

2001-11-12 · du Jin · yesky



Before introducing inline functions, it is necessary to introduce preprocessing macros. The functions of inline functions are similar to those of preprocessing macros. We believe that we have used preprocessing macros. We often define some macros, such

# Define table_comp (x)> 0? (X): 0)

A macro is defined.

 
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 function program content is executed, return to the back 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.
And affects the 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.


However, there are many unsatisfactory aspects of Macro.

1. Macros cannot access private members of objects.

2. macro definition is easy to generate two meanings.

For example:

# Define table_multi (x) (x * X)

 
We use a number to call it, table_multi (10). It seems that there is no error. The result returns 100, which is correct, but if we use
If table_multi (10 + 10) is called, the expected result is 400, while the macro call result is (10 + 10*10 + 10) and the result is 120, this is obviously not ours.
The expected result. To avoid these errors, add brackets to macro parameters.

# Define table_multi (x) * (x ))

 
This ensures that no error is made. However, even if this definition is used, this macro may still have errors. For example, if you call table_multi (A ++), they wish to get
(A + 1) * (a + 1). What is the actual result? Let's look at the macro expansion result:
(A ++) * (a ++). If the value of A is 4, the result is 5*6 = 30. The expected result is 5*5 = 25, which causes another problem. In fact, in some C library functions
These problems also exist. For example, toupper (* pchar ++) performs two ++ operations on pchar, because toupper is actually a macro.

We can see that macro has some unavoidable problems. How can we solve them?

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 used only when needed.
The function is expanded like a macro, so 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.

 
Inline functions are the most widely used in C ++ classes and should be used to define access functions. The classes we define generally define data members as private or protected, so that the outside world cannot directly read and write to us.
The data of the class member. 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.