Makeup-Understanding of "inline functions"

Source: Internet
Author: User

1. Definition:The member functions declared or defined in the class declaration are called inline functions. -- ** I think this statement is not comprehensive.

Inline functions are used to declare functions or add the "inline" mark before implementation to indicate that the functions can be processed by inline functions during compilation. But whether it is really handled by inline functions depends on the processing of the compiler. Because not all cases are suitable for using inline.

2. Example:

1. Internal declaration in the class declaration, while external definition in the class declaration is called explicit Inline Function

Class display
{
Int T;
Public:
Void output (void)
}
Display object;
Inline void display: output (void)
{
Cout <"I is" <I <"\ n ";
}
2. The internal definition of the class declaration is called an implicit inline function, for example:
Class display
{
Int T;
Public:
Inline void output (void)
{Cout <"I is" <I <"\ n ";}
}

3. Objective-how to exchange space for time

The purpose of introducing inline functions is to solve the problem of function calling efficiency in the program.

A function is a more advanced abstraction. The introduction of this function allows programmers to focus only on functions and usage of functions, rather than the specific implementation of functions. The introduction of functions can reduce the program's target code, sharing Program code and data. However, function calling also reduces the efficiency, because the calling function actually transfers the execution order of the program to an address stored in the function in the memory. After the function program content is executed, return to the place before the function is executed. This transfer operation requires that the site be protected and the address to be executed should be remembered before the transfer. After the transfer, the site should be restored and the original address should be followed for further execution. Therefore, function calling requires a certain amount of time and space overhead, which affects its efficiency. Especially for functions whose code is not very large but frequently called, it is more important to solve the efficiency problem. Inline functions are introduced to solve this problem.

During program compilation, the compiler replaces the calling expression of the inline function in the program with the function body of the inline function. Obviously, this method will not cause the problem of transfer back. However, because the code in the function body is replaced by the program during compilation, the amount of target program code will be increased and the space overhead will be increased, in terms of time consignment, it is not as big as the function call time. It can be seen that it is in exchange for time savings at the cost of the increase of the target code.

In a program, when a function is called, the function is replaced during compilation, rather than being called at runtime as a normal function.

4. Notes

Inline functions have the characteristics of general functions. They differ from general functions only in the processing of function calls. When a function is called, the execution permission of the program must be transferred to the called function, and then returned to the function that calls the function. When an inline function is called, is to replace the call expression with the inline function body. Pay attention to the following points when using inline functions:

1. Loop statements and switch statements are not allowed in inline functions. If an inline function has these statements, compile the function as a common function to generate the function call code. Recursive functions (self-called functions) cannot be used for inline functions. Inline functions are only applicable to 1 ~ Five rows of small functions. For a large function that contains many statements, the overhead of function calling and return is relatively insignificant, so it is not necessary to use inline functions.
2. the inline function must be defined before the first call of the inline function.
3. In the class structure described in this topic, all functions internally defined in the class description are inline functions.
5. In the explicit inline method in the example, "inline" is not added during function declaration, but "inline" is added during implementation ".

In the implicit inline example, the function declaration and implementation are all in the class, even if inline is not identified, it will be automatically processed by inline. Therefore, it is not a good programming style. We recommend that you use Example 1.

"Inline is a keyword used for implementation, rather than a keyword used for declaration "."

//************************************** *******************

6. Where is the implementation part of the inline function. h or. cpp?

This question is very interesting. After the test, we found that, according to the original method: in the class. h. Put the header file in the declaration, class. write the implementation part in CPP. When the main function is used in the main function, the system will prompt the link2001 error: Error lnk2001: unresolved external symbol. The solution is to write the implementation Part in the class. h header file. The reason is unknown. You are welcome to inform us.

//************************************** *******************

Book connection:

The definition of inline functions is incomplete.

It is not necessarily a class-related function, but can also be modified using inline. Example:

InlineInt maxvalue (int A, int B );//If inline is missing, it will be processed by common functions.

Int main (INT argc, char * argv [])
{
Printf ("Hello world! \ N ");
Printf ("% d", maxvalue (100,102 ));
Return 0;
}

Inline int maxvalue (int A, int B)
{
Return (A> B )? (A) :( B );
}

This is also possible.

Therefore, the implementation of inline is not limited to. H files, but can also be used in CPP. The essence of lnk2001's error is that external symbols cannot be found .......

How can we declare external symbols ??????

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.