使用inline说明的函数称内联函数。
在C++中,除具有循环语句、switch语句的函数不能说明为内联函数外,其他函数都可以说明为内联函数。
#include <iostream>
using namespace std; inline int f(int i)
{
return i * ;
} void main()
{
int a();
int b = f(a); std::cout << a << " " << b << std::endl; system("pause");
}
比C的宏更好,因为宏不进行安全检查,inline更为安全。
如果你的函数很小,比如只有两三行,或者在循环内频繁操作,可以做成inline
如果你的函数很大,比如超过20行,或者递归,不要做成inline