顶层const和底层const

清晰一组概念:

     首先只有指针才存在顶层与底层const之别(引用也可以算有(引用的本质是指针.)).

然后简述下const的用法:

const修饰的类型:

          首先看const前面是否有类型(type), 如果有就是修饰前面的type否则向后看。

实例:

const int a;//const 修饰int
int const a;//const修饰int
const int * pa;//const修饰int,底层const
int const * pa;//const修饰int,底层const
int * const pa;//const修饰int*,底层const

顶层const:修饰标识符本身类型。

底层const:修饰标识符的类型的基本类型。

如:

const int * ptr = nullptr;
//在这里const修饰的是int,而ptr的本身类型是int*
//所以const修饰的是ptr所指的底层数据
//所以const是底层const

int *const ptr = nullptr;
//在这里const修饰的是int*,ptr的本身类型是int*
//所以此处的const修饰的是ptr变量本身
//所以const是顶层const


//还是挺好理解的.

 

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值