【引用】

【引用】

概念

某个变量的引用,等价于这个变量->相当于变量别名
※注意:
1.一定要初始化成某变量
2.只能引用变量

引用作为函数返回值

#include<iostream>
using namespace std;
int n=4;
int & SetValue(){
    return n;
}
int main(){
    SetValue()=40;
    cout<<n<<endl;
    return 0;
}

image

常引用

int n;
const int &r=n;

不能通过常引用去修改其引用内容

r=200;//编译错误
n=300;//没问题->r也会被修改成5
posted @ 2025-03-13 19:08  White_ink  阅读(6)  评论(0)    收藏  举报