C ++ inline functions and inline functions

Source: Internet
Author: User

C ++ inline functions and inline functions

1. Inline Function Definition

Inline functions are basically the same as normal functions when being defined. They just add the "inline" keyword before the function value type. The definition method is as follows:

    Name of the type function of the inline function value (form parameter and its type list)

{Function body}

I am self-taught. The teaching materials may be a little old. If you have any questions, please correct me !!! Thank you !!!

 

2. Differences and relationships between inline functions and common functions

  • Added when defining inline functionsInlineKeyword;
  • The methods and methods used in the program are the same;
  • When a program calls an inline function, the code of the function is directly inserted into the call point for execution. Therefore, there is no jump or return problem of the program process during the call process; the opposite is true for common functions;
  • From the call mechanism, inline functions can accelerate the execution speed and efficiency of program code and reduce the call overhead, but increase the program code.

 

3. Restrictions on inline functions

  • A function of an inline function cannot contain complex structure control statements. If such statements are contained, the compiler generates the function call code as the function is similar to a common function;
  • Recursive functions cannot be used as inline functions;
  • Arrays cannot be described in inline functions;
  • Inline functions are generally suitable for only 1 ~ Small functions of five rows of statements.

 

Example: define a function min () to evaluate the smaller values of two integers. It must be defined as an inline function and then called in the main function.

1 # include <iostream> 2 using namespace std; 3 4 inline int min (int a, int B) {return a <B? A: B;} 5 6 int main () 7 {8 int a = 2; 9 int B = 7; 10 int c = 4; 11 cout <"a, B: "<min (a, B) <endl; 12 cout <" B, c: "<min (B, c) <endl; 13 cout <"c, a:" <min (c, a) <endl; 14 return 0; 15} 16 17 // cainiao. Please criticize and advise, coding habits and specifications !!! Thank you !!!

The running result is as follows:

A, B: 2

B, c: 4

C, a: 2

 

I am self-taught. The teaching materials may be a little old. If you have any questions, please correct me !!! Thank you !!!

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.