[C + +] inline functions

Source: Internet
Author: User

======================== Inline Function ======================
Frequent calls to small functions make function calls more expensive, and code that does not apply to functions that do the same function directly in the program can reduce the readability of the program.
The inline function solves this contradiction.
1. The Declaration of the inline function must precede the call!
For example
#include <iostream>
using namespace Std;
BOOL Isnumber (char ch); Not declared as inline here
int main ()
{
char c;
while (CIN >> c && c! = ' \ n ')
{
if (Isnumber (c))
{
cout << "digit" << Endl;
}
}

return 0;
}
inline bool Isnumber (char ch)
{
return ch >= ' 0 ' && ch <= ' 9 '? 1:0;
}
The Isnumber in the code above is not inline and is an error inline example when the compiler executes the function is treated as a normal function.

The function body of the inline function should be as small as possible should not have complex control statements such as switch or while, if these complex control statements appear in the function, then the compiler will disregard the inline declaration directly to the normal function call process (recursion can not be inline)
Experience generally suitable for small functions from 1 to 5 rows
The general use of inline functions is:
1. The function body is suitably small, which makes the embedding work easy and does not break the original calling body
2. This function is executed repeatedly in the program, especially in loops, which makes embedding more efficient.
3. The function call does not appear in many programs, so the embedding effort is relatively small and the amount of code does not soar.
==========================================================================

[C + +] inline functions

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.