const引用却指向非const对象如何理解

siyi_yihao 2012-11-22 10:19:01
C++ Primer(第四版)书上P157页有关于这个题目的解释:当使用非const对象初始化const对象的引用时,系统将非const对象转换为const对象。比如:
int i;
const int &j = i;//ok:convert non_const to reference to const int;
现在我的疑问是既然非const对象被转换为const对象了,为什么对其进行赋值操作还是合法的?这样岂不是能够通过修改i从而修改j的值了??
比如


#include<iostream>
using namespace std;
int main()
{
int i; //i=3;
const int &j= i;
i =20; //这句话为什么合法,i不是被转化为const对象了吗?
cout <<j<<" "<<i;//j,i都是20 - -!
system("pause");
return 0;}
...全文
269 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2012-11-23
  • 打赏
  • 举报
回复
不要纠结各种常量了,这个世界上唯一不变的就是变化。用API WriteProcessMemory还能修改正运行的其它进程的内存里面的所谓常量呢!
mujiok2003 2012-11-23
  • 打赏
  • 举报
回复
在c/c++中,const是只读的意思,不是不变的意思。因此,const引用或指针意味着不能通过该指针或引用修改原对象,不意味着原对象是不变的。
FrankHB1989 2012-11-23
  • 打赏
  • 举报
回复
//ok:convert non_const to reference to const int; 这句说的是错的,不是转换。即便是在重载的角度上,也是属于Implicit conversion sequencesStandard conversion sequences中的Exact Match阶的Identity类别,即No conversion required。 如果是static_cast<const int&>(i)或者(const int&)i等表达式,就是显式转换。
FrankHB1989 2012-11-23
  • 打赏
  • 举报
回复
引用 3 楼 su_xiaoyan 的回复:
这里就是隐式转换了,执行const int &j = i时,为了给const对象赋值,系统自动将i(非const对象)转换为const对象,作为临时赋值使用,而i并不是实际意义上的const对象
错误,这里直接满足reference-compatible,不会有额外的转换(更不会有临时对象被引入)。 非const对象可以初始化对应的const引用,这是引用的初始化的规则。非reference-related但可以隐式转换reference-compatible类型的初值符才会有转换。直接修改对象和这个引用没关系。 ISO C++11 8.5.3 4 Given types “cv1 T1” and “cv2 T2,” “cv1 T1” is reference-related to “cv2 T2” if T1 is the same type as T2, or T1 is a base class of T2. “cv1 T1” is reference-compatible with “cv2 T2” if T1 is reference-related to T2 and cv1 is the same cv-qualification as, or greater cv-qualification than, cv2. For purposes of overload resolution, cases for which cv1 is greater cv-qualification than cv2 are identified as reference-compatible with added qualification (see 13.3.3.2). In all cases where the reference-related or reference-compatible relationship of two types is used to establish the validity of a reference binding, and T1 is a base class of T2, a program that necessitates such a binding is ill-formed if T1 is an inaccessible (Clause 11) or ambiguous (10.2) base class of T2. 5 A reference to type “cv1 T1” is initialized by an expression of type “cv2 T2” as follows: — If the reference is an lvalue reference and the initializer expression — is an lvalue (but is not a bit-field), and “cv1 T1” is reference-compatible with “cv2 T2,” or — has a class type (i.e., T2 is a class type), where T1 is not reference-related to T2, and can be implicitly converted to an lvalue of type “cv3 T3,” where “cv1 T1” is reference-compatible with “cv3 T3”106 (this conversion is selected by enumerating the applicable conversion functions (13.3.1.6) and choosing the best one through overload resolution (13.3)), (其它情况太长略。)
魔w_j剑 2012-11-23
  • 打赏
  • 举报
回复
不能通过j来修改i的值,可以通过i来修改i的值,因为const定义的是j,i还是非const
woshi_hujunjun 2012-11-23
  • 打赏
  • 举报
回复
楼上的为什么不写博客嘞 写点 让我们 拜读 拜读 嘛
「已注销」 2012-11-22
  • 打赏
  • 举报
回复
这里就是隐式转换了,执行const int &j = i时,为了给const对象赋值,系统自动将i(非const对象)转换为const对象,作为临时赋值使用,而i并不是实际意义上的const对象
非完美主义者 2012-11-22
  • 打赏
  • 举报
回复
把引用看做一个初始化后不能再改变的指针的话,其实就是一个变量地址可以赋值给一个指向常量的指针。不能通过间接引用指针改变变量值,但变量本身还是可以修改的。
breakfisher 2012-11-22
  • 打赏
  • 举报
回复
i还是非const类型,将j定义为const引用,其实只是不能通过j修改i,还可以通过变量 i 来修改 i 的值

65,176

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧