C ++ Learning -- Inline Function

Source: Internet
Author: User

 

Inline function-the function declared with Inline is an inline function.

1. Why use inline functions?

-- In a program, a function is frequently called. During the running process, the program needs to jump to the function to execute the call. When the jump Reaches hundreds of times, will seriously affect the execution efficiency of the program.

2. Use of inline functions

In C ++, to solve this problem, use the keyword inline to declare a function, such:

Inline int func ();

The compiler will not create a real function, but copy all the code of this inline function to the calling function. In this way, the program does not need to jump back and forth during execution, thus improving the program running efficiency.

Note: inline functions can be used only when they need to be frequently called and the code is relatively simple.

3. Separation and merging of member function definitions and declarations

In general, we separate the definition and declaration:

Class A {public: inline void func (INT, INT); // The member function func () is the inline function void print (); // The member function print () is the inline function PRIVATE: int I; Int J ;}; void a: func (int x, int y) {I = x; j = y ;} void :: print () {STD: cout <"The point is" <"(" <I <"," <j <") \ n ";}

In this way, both the member functions func () and print () are inline functions.

 

When we merge the definition and declaration of member functions:

Class A {public: void func (int x, int y) {I = x; j = y} // The member function func () is the inline function void print () {STD:: cout <"The point is" <"(" <I <"," <j <") \ n ";} // The member function print () is an inline function PRIVATE: int I; Int J ;};

When the definition and declaration are merged, the member functions func () and print () are also inline functions by default!

-- Learn C ++ from scratch

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.