顶层底层const

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Lcharon/article/details/85215512

概念

以指针为例
顶层const (top level const) 指针本身是常量
底层const (low level const) 指针所指对象是常量

实例

int i = 0;
int *const p1 = &i; //顶层const,p1不可以改变,p1指向的对象可以改变
const int ci = 0; //顶层const,ci值不可以改变
const int *p2 = ci; //底层const, p2可以改变,p2指向 的对象不可以改变

其他

执行对象的拷贝操作的时候,顶层和底层const区分明显
底层const限制不能忽视,非const可以转成const

猜你喜欢

转载自blog.csdn.net/Lcharon/article/details/85215512