6.常量引用
2021-01-16 本文已影响0人
lxr_
#include<iostream>
using namespace std;
void showValue(const int& val)
{
//val = 1000;出错,不可修改
cout << "val=" << val << endl;
}
int main()
{
//常量引用:一般用来修饰形参,防止误操作
//int& ref1 = 10;//引用必须引用一块合法的内存空间
const int& ref = 10;//加上const之后,编译器将代码修改为 int temp=10;const int& ref=temp;
//ref = 20;//加入const之后,为只读状态,不允许修改
int a = 100;
showValue(a);
system("pause");
return 0;
}