inline函数:内联函数
C语言使用define函数。
这样的话,速度较慢。
#include <stdlib.h>
#include <iostream>
//替换
#define GETX3(N) N*N*N
//1+2*1+2*1+2
//函数
int getX3(int x)
{
return x*x*x;
}
//define可以实现泛型
//define缺点:不会进行类型的安全检查
void main()
{
std::cout << GETX3(1 + 2) << std::endl;
std::cout << GETX3((1 + 2)) << std::endl;
std::cout << GETX3((2.0 + 2)) << std::endl;
system("pause");
}
#include <stdlib.h>
#include <iostream>
//替换
#define GETX3(N) N*N*N
//1+2*1+2*1+2
//函数
//inline对于编译器有些时候只是一个建议。
//一般情况下,对内联函数的限制
//1 不能有递归
//2 不能包含静态数据
//3 不能包含循环
//4 不能包含switch和goto语句
//5 不能包含数组
//在程序内部展开,加快速度。
inline int getX3(int x);//内部展开,inline好处:
inline int getX3(int x)//保证类型安全
{
return x*x*x;
}
template <class T>
inline T getX2(T x)//C++类型不匹配出错,不是单纯的替换。
{
return x*x;
}
//define可以实现泛型
//define缺点:不会进行类型的安全检查
void main()
{
std::cout << getX3(1 + 2) << std::endl;
std::cout << getX3((1 + 2)) << std::endl;
std::cout << getX3((2.0 + 2)) << std::endl;
system("pause");
}
版权声明:本文为qq_38436175原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。