简简单单学习Cpp-内联函数
简单使用
- 普通函数前面加上inline就是向编译器申请成为内联函数
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <ctime>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <queue>
#include <limits>
using namespace std;
// 在普通函数面前 加上inline是向编译器申请成为内联函数
inline int Add(int x, int y) {
return x + y;
}
void test() {
int ref = Add(10, 20) * 2;
cout << ref << endl;
}
int main() {
// 普通引用
test();
system("pause");
}
什么时候不会成为内联函数
- 存在过多的条件判断语句
- 函数体过大
- 对函数进行取地址操作
内联函数的好处
- 有宏函数的效率 没有宏函数的缺点
- 类的成员函数默认加上inline