• C++


    第一眼见到explicit和volatile可能会一愣一愣的觉得可能是c11或者c14新加的标识符。

    其实不是这样,volatile和const两个关键字在C语言的第二个版本KR C的时候就被加入了C标准,他们是两个相对的关键字

    const 修饰符表示这是一个常量类型,这个变量的值不会被程序改变
    
    volatile 修饰符表示这个变量可能被编译器以外的行为(譬如内联汇编程序)改变。

    修饰常量变量只要和类型紧挨着就可以

    int const a = 1;
    const int a = 1;

    修饰指针时以*号为分界符号

    #include <iostream>
    #include <iostream>
    
    int main() {
        int a = 10;
        int b = 20;
    
        const int *p1 = &a;
        int const *p2 = &a;
        int *const p3 = &a;
        const int *const p4 = &a;
        int const *const p5 = &a;
    
        printf("&a = %X
    ", &a);
        printf("&b = %X
    ", &b);
    
        printf("p1 = 0x%X , *p1 = %d
    ", p1, *p1);
        printf("const int *p1 = &a; 
     (*p1) = b;通过p修改a的值
    ");
    //    (*p1) = b;
        p1 = &b;
        printf("p1 = 0x%X , *p1 = %d
    ", p1, *p1);
        printf("p2 = 0x%X , *p2 = %d
    ", p2, *p2);
        printf("int const *p2 = &a; 
     (*p2) = b;通过p修改a的值
    ");
    //    (*p2) = b;
        p2 = &b;
        printf("p2 = 0x%X , *p2 = %d
    ", p2, *p2);
        printf("p3 = 0x%X , *p3 = %d
    ", p3, *p3);
        printf("int *const p3 = &a; 
     p3 = &b;修改p的指向
    ");
    //    p3 = &b;
        (*p3) = b;
        printf("p3 = 0x%X , *p3 = %d
    ", p3, *p3);
        printf("p4 = 0x%X , *p4 = %d
    ", p4, *p4);
        printf("const int *const p4 = &a; 
     二者都不能修改
    ");
    //    (*p4) = &b;
    //    p4 = &b;
        printf("p4 = 0x%X , *p4 = %d
    ", p4, *p4);
        printf("p5 = 0x%X , *p5 = %d
    ", p5, *p5);
        printf("int const *const p5 = &a; 
     二者都不能修改
    ");
    //    (*p5) = &b;
    //    p5 = &b;
        printf("p5 = 0x%X , *p5 = %d
    ", p5, *p5);
        return 0;
    }

    gcc报错结果

    J:SITPalgmain.cpp: In function 'int main()':
    J:SITPalgmain.cpp:14:11: error: assignment of read-only location '* p1'
         (*p1) = b;
               ^
    J:SITPalgmain.cpp:17:11: error: assignment of read-only location '* p2'
         (*p2) = b;
               ^
    J:SITPalgmain.cpp:20:8: error: assignment of read-only variable 'p3'
         p3 = &b;
            ^
    J:SITPalgmain.cpp:23:11: error: assignment of read-only location '*(const int*)p4'
         (*p4) = &b;
               ^
    J:SITPalgmain.cpp:23:11: error: invalid conversion from 'int*' to 'int' [-fpermissive]
    J:SITPalgmain.cpp:24:8: error: assignment of read-only variable 'p4'
         p4 = &b;
            ^
    J:SITPalgmain.cpp:26:11: error: assignment of read-only location '*(const int*)p5'
         (*p5) = &b;
               ^
    J:SITPalgmain.cpp:26:11: error: invalid conversion from 'int*' to 'int' [-fpermissive]
    J:SITPalgmain.cpp:27:8: error: assignment of read-only variable 'p5'
         p5 = &b;
            ^
    mingw32-make.exe[3]: *** [CMakeFiles/alg.dir/main.cpp.obj] Error 1
    mingw32-make.exe[2]: *** [CMakeFiles/alg.dir/all] Error 2
    mingw32-make.exe[1]: *** [CMakeFiles/alg.dir/rule] Error 2
    CMakeFilesalg.diruild.make:61: recipe for target 'CMakeFiles/alg.dir/main.cpp.obj' failed
    CMakeFilesMakefile2:66: recipe for target 'CMakeFiles/alg.dir/all' failed
    CMakeFilesMakefile2:78: recipe for target 'CMakeFiles/alg.dir/rule' failed
    mingw32-make.exe: *** [alg] Error 2
    Makefile:117: recipe for target 'alg' failed

    将非法的行注释掉之后可以看到允许修改的内容

    J:SITPalgcmake-build-debugalg.exe
    &a = 72FE24
    &b = 72FE20
    p1 = 0x72FE24 , *p1 = 10
    const int *p1 = &a;
     (*p1) = b;通过p修改a的值
    p1 = 0x72FE20 , *p1 = 20
    p2 = 0x72FE24 , *p2 = 10
    int const *p2 = &a;
     (*p2) = b;通过p修改a的值
    p2 = 0x72FE20 , *p2 = 20
    p3 = 0x72FE24 , *p3 = 10
    int *const p3 = &a;
     p3 = &b;修改p的指向
    p3 = 0x72FE24 , *p3 = 20
    p4 = 0x72FE24 , *p4 = 20
    const int *const p4 = &a;
     二者都不能修改
    p4 = 0x72FE24 , *p4 = 20
    p5 = 0x72FE24 , *p5 = 20
    int const *const p5 = &a;
     二者都不能修改
    p5 = 0x72FE24 , *p5 = 20
    
    Process finished with exit code 0
  • 相关阅读:
    Arcgis地理处理工具案例教程——批量提取多个栅格的范围边界矢量
    win10 windows10自动修复系统
    ArcGIS案例教程—E00格式批量导入成要素类
    Python正则表达式多行匹配问题
    学习ArcGIS开发的途径有哪些
    Arcgis空间分析案例教程——计算空间分布范围
    ArcGIS地理建模批处理教程——批量添加文件名作为字段值
    ArcGIS地理建模批处理教程——批量去除Z值和M值
    华为NM存储卡与Micro SD存储卡对比分析
    EDT
  • 原文地址:https://www.cnblogs.com/liutianchen/p/8716638.html
Copyright © 2020-2023  润新知