In-depth analysis of inline functions

Source: Internet
Author: User

The inline function is only a suggestion for the compiler. the compiler can ignore this suggestion. For example, if you specify a 1000-row function as inline, the compiler will ignore this inline, returns this function to a common function. (From Baidu encyclopedia )..

I 'd like to describe several examples of the inline function: (from Baidu encyclopedia)

1. define this function in the class: Class classname {......... int getwidth () {return m_lpicwidth;}; // if it is directly defined in the class, it does not need to be modified using inline. The Compiler automatically uses the inline function ....} 2. add the inline Keyword: Class account {public: Account (double initial_balance) {balance = initial_balance;} // double getbalance (); // declare double deposit (double amount); double withdraw (double amount); Private: Double balance ;}; inline double account: getbalance () {return balance;} in the class ;}/ /Add the inline keyword inline double account: Deposit (double amount) {return (balance + = amount);} inline double account: withdraw (double amount) when defining a class) {return (balance-= amount);} There are also some rules to note: 1. The Inline description is only a suggestion for the compiler. the compiler can ignore this suggestion. For example, if you specify a function with more than 1000 rows as inline, the compiler will ignore this inline and restore this function to a normal function. 2. When calling an inline function, make sure that the inline function definition is visible to the compiler. That is to say, the inline function definition must be included in the header file, which is different from the common function definition. However, if you are used to placing the function definition in the CPP file or want to make the header file more concise, you can do this: // someinline. in H # ifndef someinline_h # define someinline_hinline type example (void );//........ declaration of other functions # include "someinlie. CPP "// The source file suffix varies with the compiler # endif // someinline. # include "someinline. H "type example (void ){//..........} //............... the preceding methods of other functions are generic and effective, so you can use them with confidence. You do not have to worry about the compilation errors caused by containing CPP files in the header file. As I saw in the book:1: The Inline behavior of the inline function is mostly the compiler behavior in the C ++ program. Therefore, for all virtual functions, the intention to call inlinine fails. Because Virtual uses multi-state behavior, it can be determined to call the function at runtime. 2: The Compiler usually does not implement inlining3 for "calling through function pointers": no matter how tight the work compiled in the constructor or destructor is (less code ), use inlining with caution, because the derived function will at least call the constructors or destructor of its member variables and base class one after another ..

4: The inline function cannot be upgraded with the library upgrade. If F is an inline function, if f changes, all client programs that use F must be re-compiled. This is often not expected. However, if F is a non-inline function, once it has any modifications, the client only needs to reconnect, it is far less costly than re-compilation. If the library deletes dynamic connections, the upgraded functions can even be absorbed by the application without knowing it.

5: Most debuggers are helpless in the face of inline functions. After all, how do you set breakpoints in a function that does not exist?

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.