C_and_CPP 板


LINE

开发平台(Platform): (Ex: VC++, GCC, Linux, ...) G++, Linux 额外使用到的函数库(Library Used): (Ex: OpenGL, ...) 问题(Question): 请问各位大大,为什麽这段程式码 compile 不过? 小弟跪求解释 int main() { int *b = 0; const int *& n = b; } 错误讯息: error: invalid initialization of non-const reference of type 'const int*&' from an rvalue of type 'const int*' 我有找到这篇说是因为type-safe 的关系: http://stackoverflow.com/a/11514730 但是如果把程式改成这样,也没有type-safe,可是却可以成功compile int a = 0; int *b = &a; const int & n = *b; cout << n << endl; // n = 0 *b = 3; cout << n << endl; // n = 3 又看到了这篇的回答:http://stackoverflow.com/a/31412466 但是却也看不太懂他的回答是什麽意思,为什麽它会回传rvalue? 还有,为什麽宣告成 const int * const & n = b 就可以compile 过? 感谢各位大大! 程式码(Code):(请善用置底文网页, 记得排版) http://ideone.com/W5kqRr 补充说明(Supplement): --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 220.135.48.199
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1475230865.A.28A.html
1F:→ pttworld: 「指」标要指得到东西,什麽被指到。 09/30 18:24
这边的指标指到 NULL 啊 ※ 编辑: seanooxox (220.135.48.199), 09/30/2016 18:33:47
2F:推 shadow0326: int* const & n = b; 09/30 18:48
3F:→ shadow0326: 就是const修饰到什麽的问题 09/30 18:48
4F:→ pttworld: const int *b = 0; 09/30 19:06
5F:→ seanooxox: 0 在这边就是NULL的意思啊,而且就算换成其他东西也会 09/30 20:17
6F:→ seanooxox: 有一样的错误 09/30 20:17
7F:→ pttworld: 可以尝试全篇翻英丢上,语法解法已给。问题本身不明确。 09/30 20:36
8F:→ seanooxox: 先感谢您的回覆,可能是我表达的不清楚。那我换个方式 09/30 20:57
9F:→ seanooxox: 问问题好了,在我程式码中红色字的那一行,如果拿掉con 09/30 20:57
10F:→ seanooxox: st的修饰,就会变得可以compile,我想知道这个跟rvalue 09/30 20:57
11F:→ seanooxox: 有什麽关联 09/30 20:57
12F:→ pttworld: lr以assignment hand side判定。 09/30 21:46
13F:→ pttworld: 只是编译器认为指定失败,不能,不过而已。 09/30 21:48
14F:→ klsdf: 就我的理解是 (const int) * & 你用一个型态指向const int 09/30 22:27
15F:→ klsdf: 的指标去ref一个int *本来就会跟你说型态错误 09/30 22:28
16F:→ klsdf: 你用(const int) (* const) & 可以过是因为你的pointer是 09/30 22:29
17F:→ klsdf: const不可更改 ref的语意有被推导出来 等其他强者解释 QQ 09/30 22:35
18F:→ klsdf: 就是const int *&是lvalue ref 他应该是a等效为b 09/30 22:37
19F:→ klsdf: 但a是const int, 你*b改了值 *a也会跟着改就语意矛盾 09/30 22:38
20F:推 ralts: b 会先 implicit 转型成 const int* 的 rvalue, 而错误在 r 10/01 03:05
21F:→ ralts: value 只能有 const reference. 10/01 03:05
22F:→ g0010726: 一开始的code红色行给你编译过的话 你就可以做: 10/01 08:20
23F:→ g0010726: const int cint; n = &cint; 10/01 08:21
24F:→ g0010726: *b = 100; //改到cint惹 爆炸 10/01 08:22
25F:→ g0010726: 也就是 想在type的某level加上const,得要一路往上每个 10/01 08:27
26F:→ g0010726: level加const。 除了top level 可以不用管 10/01 08:27







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Tech_Job站内搜寻

TOP