======================== 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