参考变量 [英] reference variable

查看:145
本文介绍了参考变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些文本中,我们不能给引用变量赋值常量值。当我执行这样的程序,我可以做到。是否有任何条件,我们不能为引用变量赋一个常量值?

In some text it is given that we can't assign constant values to a reference variable. When I executed such a program I could do it. Is there any condition we can't assign a constant value to a reference variable?

推荐答案

const int &i = 12;

如果引用不是const,则会出现编译错误。

If the reference is not const, you get a compiler error.

int &i = 12; //compiler error

常数值(大多数时候)只有内存段。因此,你不能使用非const引用来引用它们,因为这意味着你可以修改它们。

Constant values (e.g. literals) are (most of the time) stored in read-only segments of the memory. Consequently, you can't reference them using non-const references, because that would mean you could modify them.

这篇关于参考变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆