美文网首页
顶层const

顶层const

作者: 阿布554_ | 来源:发表于2017-07-06 14:50 被阅读87次

    说到顶层const(top-level const)在c++ primer 第五版里面有介绍到

    用名词顶层const(top-level const)表示指针本身是个常量,而用底层const(low-level const)表示指针所指的对象是一个常量

    int *const p1 = &a                   //是一个顶层const,不能改变指针p1的值,也就是说p1这一辈子都指向a
    const int *p1 = &a                   //一个底层const,所指的对象是一个常量
    

    但是实际上定义这样一个概念反倒让人觉得迷惑,所以最好的办法就是从右往左读这个表达式

    int *const p1读作p1是一个常量指针指向int类型
    const int *p1读作p1是一个指针指向一个常量int类型

    相关文章

      网友评论

          本文标题:顶层const

          本文链接:https://www.haomeiwen.com/subject/vjmhhxtx.html