Introduction to the correct use of C ++ inline functions

Source: Internet
Author: User

(1) c ++ inline functions
In the function declaration or definition, add the keyword inline before the function return type to specify min () as inline.
Inline int min (INT first, int secend ){/****/};
The inline function must be visible to the compiler so that it can expand the function within the call point. Unlike non-inline functions, inline functions must be defined in each text file that calls the function. Of course, for different files of the same program, if the C ++ inline function appears, its definition must be the same. For compute. C and draw. for programs composed of C, programmers cannot define such a min () function. c Indicates one thing, but in draw. C refers to another thing. If the two definitions are different, the program will have undefined behavior:
To ensure that this will not happen, we recommend that you put the inline function definition in the header file. This header file is contained in each file that calls the inline function. This method ensures that there is only one definition for each inline function, and the programmer does not need to copy the code, and it is impossible to cause unintentional mismatch in the life of the program.

(2) programming style of inline functions
The keyword inline must be put together with the function definition body to make the function inline. It does not work unless you put inline before the function declaration.
The following function Foo cannot be an inline function:

Inline void Foo (int x, int y); // inline is put together only with the function declaration void Foo (int x, int y ){}

The following function Foo becomes an inline function:

Void Foo (int x, int y); inline void Foo (int x, int y) // put inline together with the function definition body {}

Therefore, the C ++ inline function is a "keyword used for implementation" instead of a "keyword used for Declaration ". Generally, you can read the declaration of a function, but cannot see the definition of a function. Although the inline keyword is added before the declaration and definition body of inline functions in most textbooks, I don't think inline should appear in the declaration of functions. Although this detail does not affect functions of functions, it reflects a basic principle of high quality c ++/C programming style: Declarations and definitions cannot be confused, the user does not need to know whether the function needs to be inline.
The member functions defined in the class declaration will automatically become inline functions, for example:

Class A {public: void Foo (int x, int y) {}// automatically become an inline function}

Putting the definition body of the member function in the class declaration can bring convenience in writing, but it is not a good programming style. The above example should be changed:

// Header file class A {public: void Foo (int x, int y);} // definition file inline void a: Foo (int x, int y ){}

(From: http://developer.51cto.com/art/201002/182089.htm)

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.