鱼C论坛

 找回密码
 立即注册
查看: 2288|回复: 9

[已解决]C++ const

[复制链接]
发表于 2019-6-16 15:10:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 我就是个弟弟 于 2019-6-16 15:35 编辑
  1. #include <iostream>

  2. using namespace std;

  3. int main(int, char **)
  4. {
  5.     const int number = 1234;

  6.     //如何修改number的值,可以用指针

  7.     return 0;
  8. }
复制代码

考考大家,看看大家对于C++和const的理解。
最佳答案
2019-6-16 16:24:33
这样编译器就只能老老实实的传递number的地址
number变量的值已经由const_cast修改为100

  1. #include <iostream>

  2. void print(const int *number)
  3. {
  4.         std::cout << *number << std::endl;
  5. }

  6. int main(void)
  7. {
  8.         const int number = 1234;
  9.         *const_cast<int *>(&number) = 100;
  10.         print(&number);
  11.         return 0;
  12. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-6-16 15:35:56 | 显示全部楼层
  1.         const int number = 1234;
  2.         int* a = (int *)&number;
  3.         *a = 666;
  4.         printf("%d\n", number);
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-6-16 15:52:07 | 显示全部楼层

非常接近答案了,大佬
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-16 15:56:05 | 显示全部楼层
我就是个弟弟 发表于 2019-6-16 15:52
非常接近答案了,大佬

难道不对?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-6-16 15:56:56 | 显示全部楼层

编译会优化滴
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-16 16:19:10 | 显示全部楼层
是要考const_cast吗?^_^

  1. *const_cast<int *>(&number) = 100;
复制代码

从反汇编来看,这样的确可以修改number变量

  1. std::cout << number << std::endl;
复制代码

如果只是这样输出number,从反汇编来看,编译器并没有访问number变量,编译器直接把number替换成了常量1234
所以输出时还要再来一次const_cast

  1. #include <iostream>

  2. int main(void)
  3. {
  4.         const int number = 1234;
  5.         *const_cast<int *>(&number) = 100;
  6.         std::cout << *const_cast<int *>(&number) << std::endl;
  7.         return 0;
  8. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-16 16:24:33 | 显示全部楼层    本楼为最佳答案   
这样编译器就只能老老实实的传递number的地址
number变量的值已经由const_cast修改为100

  1. #include <iostream>

  2. void print(const int *number)
  3. {
  4.         std::cout << *number << std::endl;
  5. }

  6. int main(void)
  7. {
  8.         const int number = 1234;
  9.         *const_cast<int *>(&number) = 100;
  10.         print(&number);
  11.         return 0;
  12. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-16 16:31:06 | 显示全部楼层
人造人 发表于 2019-6-16 16:24
这样编译器就只能老老实实的传递number的地址
number变量的值已经由const_cast修改为100

厉害!
所以说如果直接是输出number这个标识符的话还是逃不过c++的编译器是吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-16 18:12:05 | 显示全部楼层
newu 发表于 2019-6-16 16:31
厉害!
所以说如果直接是输出number这个标识符的话还是逃不过c++的编译器是吗


嗯,编译器会进行优化

  1. #include <iostream>

  2. int main(void)
  3. {
  4.         const int number = 1234;
  5.         *const_cast<int *>(&number) = 100;
  6.         std::cout << number << std::endl;
  7.         return 0;
  8. }
复制代码

  1. main:
  2. .LFB1494:
  3.         .file 1 "main.cpp"
  4.         .loc 1 4 0
  5.         .cfi_startproc
  6.         pushq        %rbp
  7.         .seh_pushreg        %rbp
  8.         .cfi_def_cfa_offset 16
  9.         .cfi_offset 6, -16
  10.         movq        %rsp, %rbp
  11.         .seh_setframe        %rbp, 0
  12.         .cfi_def_cfa_register 6
  13.         subq        $48, %rsp
  14.         .seh_stackalloc        48
  15.         .seh_endprologue
  16.         .loc 1 4 0
  17.         call        __main
  18. .LVL0:
  19.         .loc 1 5 0
  20.         movl        $1234, -4(%rbp)                # 这里number初始化成1234
  21.         .loc 1 6 0
  22.         leaq        -4(%rbp), %rax
  23.         movl        $100, (%rax)                # 这里把number修改成100
  24.         .loc 1 7 0
  25.         movl        $1234, %edx                # 这里直接把1234传给std::cout,并没有访问number变量
  26.         movq        .refptr._ZSt4cout(%rip), %rcx
  27.         call        _ZNSolsEi
  28.         movq        .refptr._ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_(%rip), %rdx
  29.         movq        %rax, %rcx
  30.         call        _ZNSolsEPFRSoS_E
  31.         .loc 1 8 0
  32.         movl        $0, %eax
  33.         .loc 1 9 0
  34.         addq        $48, %rsp
  35.         popq        %rbp
  36.         .cfi_restore 6
  37.         .cfi_def_cfa 7, 8
  38.         ret
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-16 18:15:56 | 显示全部楼层
newu 发表于 2019-6-16 16:31
厉害!
所以说如果直接是输出number这个标识符的话还是逃不过c++的编译器是吗
  1. #include <iostream>

  2. int main(void)
  3. {
  4.         const int number = 1234;
  5.         *const_cast<int *>(&number) = 100;
  6.         std::cout << *const_cast<int *>(&number) << std::endl;
  7.         return 0;
  8. }
复制代码

  1. main:
  2. .LFB1494:
  3.         .file 1 "main.cpp"
  4.         .loc 1 4 0
  5.         .cfi_startproc
  6.         pushq        %rbp
  7.         .seh_pushreg        %rbp
  8.         .cfi_def_cfa_offset 16
  9.         .cfi_offset 6, -16
  10.         movq        %rsp, %rbp
  11.         .seh_setframe        %rbp, 0
  12.         .cfi_def_cfa_register 6
  13.         subq        $48, %rsp
  14.         .seh_stackalloc        48
  15.         .seh_endprologue
  16.         .loc 1 4 0
  17.         call        __main
  18. .LVL0:
  19.         .loc 1 5 0
  20.         movl        $1234, -4(%rbp)
  21.         .loc 1 6 0
  22.         leaq        -4(%rbp), %rax
  23.         movl        $100, (%rax)
  24.         .loc 1 7 0
  25.         leaq        -4(%rbp), %rax                        # 这里访问number变量
  26.         movl        (%rax), %eax
  27.         movl        %eax, %edx
  28.         movq        .refptr._ZSt4cout(%rip), %rcx
  29.         call        _ZNSolsEi
  30.         movq        .refptr._ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_(%rip), %rdx
  31.         movq        %rax, %rcx
  32.         call        _ZNSolsEPFRSoS_E
  33.         .loc 1 8 0
  34.         movl        $0, %eax
  35.         .loc 1 9 0
  36.         addq        $48, %rsp
  37.         popq        %rbp
  38.         .cfi_restore 6
  39.         .cfi_def_cfa 7, 8
  40.         ret
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-1 16:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表