美文网首页
6.常量引用

6.常量引用

作者: lxr_ | 来源:发表于2021-01-16 11:29 被阅读0次
#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;
}

相关文章

网友评论

      本文标题:6.常量引用

      本文链接:https://www.haomeiwen.com/subject/kkduaktx.html