示例:
#include <iostream>
// 关键字 inline 代码块必须简短
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);
cout << "a = " << a << ", b = " << b << "\n";
cout << "c = " << c;
cout << ", c squared = " << square(c++) << "\n";
cout << "Now c = " << c << "\n";
return 0;
}
引用
#include <iostream>
int main()
{
using namespace std;
int rats = 101;
int& rodents = rats;
cout << "rats = " << rats;
cout << ", rodents = " << rodents << endl;
rodents++;
cout << "rats = " << rats;
cout << ", rodents = " << rodents << endl;
cout << "rats address = " << &rats;
cout << ", rodents address = " << &rodents << endl;
return 0;
}
其实引用是指针的伪装:
int rats = 101;
int& rodents = rats;
int* const prats = &rats 要留意 const是在里面 也就是说 该指针变量不能指向其他地址
parts = rodents
引用变量无法通过赋值语句改变其内存地址 且指针不允许赋值给引用变量。
C++标准中 对于函数形参使用const 修饰符的引用变量 允许传递类型不正确或不是左值的数据。因为C++会创建临时变量
如果没有const修饰符的话 则不允许这么干(有一些旧的编译器只是报错 但是依然通过编译)
four为结构类型的变量
accumulate返回dup(与four是同一种结构数据类型)的引用
所以如下的赋值语句的顺序是:
accumulate(dup, five) = four;
==
dup = accumulate(dup, five)
dup = four