C ++ inline function, inline function

Source: Internet
Author: User

C ++ inline function, inline function

This article mainly records the inline function in C ++, that is, the inline function, and mainly records the following issues:

The main purpose is to use it to replace the macro definition in the expression form in the C language to solve the function call efficiency problem in the program.

Macro definition in C language: # define ExpressionName (var1, var2) (var1 + var2) * (var1-var2) macro definition, which is implemented using a Preprocessor, without a series of operations such as parameter pressure stack and code generation, the efficiency is very high. But the disadvantages are as follows:

  • It is only a simple replacement in the pre-processor symbol table. Therefore, parameter validity cannot be checked, and the strict type check of the C ++ compiler cannot be enjoyed.
  • In addition, its return value cannot be forcibly converted to a suitable type for conversion.
  • In addition, C ++ introduces access control for classes and classes. In this way, if an operation or expression involves protection members or private members of a class, this macro definition cannot be implemented (because this pointer cannot be placed in a proper position)
The sample code above shows that the two member functions of Class A are all inline functions, and the readTest () function is defined in the class, so it is automatically converted to inline function, setTest () the function is defined outside the class, so the inline keyword must be added. The members of a class are often defined as protected and private. They cannot be directly accessed from outside, so they must be accessed by member interface functions. These interface functions are defined as inline functions to achieve better results. Therefore, the inline function is often used to define the access function (the code is short), and the efficiency of the inline function is relatively high.

Inline is code expansion (replication) to be seen. It only saves the overhead of function calls, thus improving the function execution efficiency. If the execution time of the Code in the function body is higher than the overhead of the function call, the efficiency gains will be little. On the other hand, every inline function call needs to copy the Code to increase the total amount of code in the program and consume more memory space.
Therefore:

  • When the amount of code for a function is large, using the inline function will cause high memory consumption.
  • If a loop occurs in the function body, the execution time of the function is higher than that of the function call.
  • In addition, constructor and destructor of classes are easy to misunderstand that using inline is more effective. Be careful that constructors and destructor may hide some behaviors, such as "secretly executing constructors and destructor of base classes or member objects ."

A good compiler will automatically cancel unsuitable inline functions based on the function body. (It indicates that inline should not appear inside the class and the declaration part of the function)

5. What is the difference between inline functions and Macros?

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.