1. const的好处就在于可以固定为常量;
2.引用 &,引用与指针的操作的区别在于,指针传入函数之后,指针还会创建一个本地的拷贝,而引用直接是变量的别名,不会被拷贝;
3. inline,内联函数,是建议编译器进行展开,而不一定就一定被展开了。内联与普通函数的区别在于,内联函数是直接被就地展开了,类似于复制粘贴。而普通的函数会存在函数调用,压栈出栈那一系列的操作。
4. static inline, 不同的.c文件中,可能创建了很多同名的内联函数,使用这个前缀,保证当前的内联函数仅在当前.c中展开。
#include <iostream>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int answer;
int answer_1;
int & multiply (const int & a, const int &b)
{
answer_1 = a*b;
return answer_1;
}
const int & multiply_const (const int & a, const int &b)
{
answer = a*b;
return answer;
}
int main(int argc, char** argv) {
int x=5,y=10;
int & z=multiply(x,y);
printf("z %d answer_1 %d\n",z,answer_1);
z = 10;
printf("z %d answer_1 %d\n",z,answer_1);
z=multiply_const(x,y);
printf("z %d answer %d\n",z,answer);
z = 10;
printf("z %d answer %d\n",z,answer);
return 0;
}
运行:
重点在于最后一次打印,常量引用的answer没有被改变值。