C++编程基础二 08-内联函数

 1 // C++函数和类 08-内联函数.cpp: 定义控制台应用程序的入口点。
 2 //
 3 
 4 #include "stdafx.h"
 5 #include <iostream>
 6 #include <string>
 7 #include <array>
 8 #include <climits>
 9 #include <math.h>
10 using namespace std;
11 
12 //内联函数是C++为提高程序运行速度所做的一项改进。
13 //内联函数的编译代码与其他程序代码“内联”起来了,也就是说,
14 //编译器将使用相应的函数代买替代函数调用。
15 //对于内联代买,程序无需调到另一个位置处执行代买,再跳回来。
16 //因此,内联函数的运行速度比常规函数稍快,但代价是需要占用更多的内存。
17 //内联函数的使用方法:在函数声明或函数定义前加上关键字inline。
18 //通常的做法是省略原型,将这个定义放在本应提供原型的地方。
19 //注意:内联函数不能递归。
20 inline int sum(int a, int b) { return a + b; }
21 int main()
22 {
23     int res = sum(20, 45);
24     cout << res << endl;
25     return 0;
26 }
27  

猜你喜欢

转载自www.cnblogs.com/uimodel/p/9348590.html