这样解释吧,因为C++使用了太多的成员函数这中东西而一些功能是经常要用到的,就用inline关键字申明,这样的函数与一般函数不同之处就在于,在编译是这些函数的主体也就是功能部分直接插入了程序的代码之中,而没有函数调用的堆栈生成、销毁的过程,同时没有了过程调用语句也可以是CPU的执行效率提高。举例来说:
有这样一个函数:
int max(int x,y)
{
if (x>=y)return x else return y;
}
如果在程序的其他地方调用此函数,就回把if (x>=y)return x else return y;编译后的二进制代码插入该处,而不是象一般的插入函数调用的代码。
另外在C++中,在类声明部分定义的函数自动转为内联函数。
inline Specifier
The inline specifier instructs the compiler to replace function calls with the code of the function body. This substitution is “inline expansion” (sometimes called “inlining”). Inline expansion alleviates the function-call overhead at the potential cost of larger code size.
The inline keyword tells the compiler that inline expansion is preferred. However, the compiler can create a separate instance of the function (instantiate) and create standard calling linkages instead of inserting the code inline. Two cases where this can happen are:
Recursive functions.
Functions that are referred to through a pointer elsewhere in the translation unit.
Note that for a function to be considered as a candidate for inlining, it must use the new-style function definition. Functions that are declared as inline and that are not class member functions have internal linkage unless otherwise specified.