相关疑难解决方法(0)

什么是常量参考?(不是对常数的引用)

一个相当理论的问题......为什么常量引用的行为与常量指针的行为不同,我实际上可以更改它们指向的对象?它们看起来像是另一个简单的变量声明.我为什么要用它们?这是我运行的一个简短示例,它编译并运行时没有错误:

int main (){
    int i=0;
    int y=1;    
    int&const icr=i;
    icr=y;          // Can change the object it is pointing to so it's not like a const pointer...
    icr=99;         // Can assign another value but the value is not assigned to y...
    int x=9;
    icr=x;
    cout<<"icr: "<<icr<<", y:"<<y<<endl; 
}
Run Code Online (Sandbox Code Playgroud)

c++ const reference constants

62
推荐指数
4
解决办法
11万
查看次数

标签 统计

c++ ×1

const ×1

constants ×1

reference ×1