《C++ primer plus》学习笔记——第八章
文章目录
一、C++内联函数
(1)C++区别于C语言,提供了新特性:内联函数,引用传递变量,默认的参数值,函数重载(多态)以及模板函数;
(2)C++内联函数的优势:
(3)写内联函数的要求:
(a)在函数声明前加上关键字inline;
(b)在函数定义前加上关键字inline;
(c)内联函数不能递归
(4)什么时候用内联函数呢?
代码量不多,且经常使用
(5)eg如下:
#include<stdio.h>
#include<stdlib.h>
// inline.cpp -- using an inline function
#include <iostream>
// an inline function definition
inline double square(double x) { return x * x; }
int main()
{
using namespace std;
double a, b;
double c = 13.0;
a = square(5.0);
b = square(4.5 + 7.5); // can pass expressions
cout << "a = " << a << ", b = " << b << "\n";
cout << "c = " << c;
cout << ", c squared = " << square(c++) << "\n";
cout << "Now c = " << c << "\n";
system("pause");
return 0;
}
说明:
(1)
(2)内联函数与宏定义的区别