Introduction to C + + inline functions (Inline)

Source: Internet
Author: User

Before introducing inline functions, it is necessary to introduce a preprocessing macro. The functionality of an inline function is similar to the functionality of a preprocessing macro. I believe you have used preprocessing macros, we will often define a number of macros, such as

#define TABLE_COMP (x) (x) >0? ( x): 0)

 
A macro is defined.

Why do you use macros? Because the invocation of a function must move the order of the program execution to an address in memory where the function is stored, the function's program contents are executed, and then returned to the place before the function is transferred. This transfer operation requires the preservation of the site and memory of the execution of the address before the transfer to the execution, back to the site, and continue to carry out the original save address. Therefore, the function call has a certain amount of time and space overhead, so it will affect its efficiency. A macro is more efficient at calling a macro than calling a function, except that it expands the code where it is preprocessed and does not require additional space and time overhead.

But macros also have a lot of undesirable places.

1,. Macros cannot access private members of an object.

2,. The definition of a macro can easily produce two of meaning.

Let's give an example:

#define TABLE_MULTI (x) (x*x)


We use a number to invoke it, Table_multi (10), so that there seems to be no error, the result is 100, is correct, but if we use Table_multi (10+10) to call, we expect the result is 400, and the macro call result is (10+10 *10+10), the result is 120, which is obviously not the result we are going to get. One way to avoid these errors is to add parentheses to the parameters of the macro.

#define TABLE_MULTI (x) ((x) * (x))


This ensures that there is no error, but even with this definition, the macro can still be faulted, such as using Table_multi (a++) to invoke it, and they intend to get the result (a+1) * (a+1), in fact. We can look at the expansion result of the macro: (a++) * (a++), if the value of a is 4, the result is 5*6=30. And the result we expect is 5*5=25, and there is a problem. In fact, some of the library functions in C also have these problems. For example: Toupper (*pchar++) performs two + + operations on Pchar, because Toupper is actually a macro.

We can see that the macro has some unavoidable problems, how to solve it.

Here's how to solve these problems with the inline functions I want to introduce, and we can use inline functions to replace the definition of macros. And in fact we can replace the preprocessing macros completely with inline functions.

The difference between an inline function and a macro is that the macro is replaced by the preprocessor, and the inline function is implemented through compiler control. and the inline function is the real function, only when need to use, inline function like macro expansion, so cancel the function of the parameter stack, reduce the overhead of the call. You can invoke inline functions just as you would call a function without worrying about problems that arise from dealing with macros.

We can use inline to define inline functions, but any function defined in the Description section of the class is automatically considered an inline function.

Let's introduce the use of inline functions.

Inline functions must be declared with the function body to be valid. A declaration like this inline tablefunction (int I) is ineffective, and the compiler simply declares the function as a normal function, and we must define the function body.

Inline tablefunction (int I) {return i*i};


This allows us to define an inline function. We can call it as a normal function. But the execution speed is faster than the normal function.

We can also define functions that are defined outside of the class as inline functions, 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 stated above are inline functions. In C + +, the functions of the function body are defined inside the class, and are implicitly considered to be inline functions. and whether or not you have inline keywords.

inline functions, which are most widely used in C + + classes, should be used to define access functions. The classes we define generally define the data members as private or protected, so that the outside world cannot read and write directly to the data of our class members. Read-write for private or protected members must be performed using member interface functions. If we define these read-write member functions as inline functions, we will get better efficiency.

Class sample{
Private:
Int ntest;
Public:
Int Readtest () {return ntest;}
Void settest (int I) {ntest=i}
}


Of course, inline functions also have some limitations. Is that the function of the execution code can not be too much, if the function of the inline function is too large, the general compiler will discard the inline method, and the normal way to call the function. In this way, inline functions are as efficient as normal function execution.

This article turns from: http://www.yesky.com/221/204721.shtml

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.