const 加 pointer,常量指针与指针常量的区别

 int * const p 和 const int * p;这两者哪个是常量指针哪个是指针常量,实在是容易搞混;
 按照英文原文,感觉好理解点:
 int * const p  --->  const pointer; 称为常量指针,应该没问题;或者就按英文来记;
 const int * p  --->  pointer to const; 称为 指向常量的指针, 虽然字数多,可好理解。。。

1. effictive c++ 条款 3 中: 如果关键字出现在星号(*)左边, 表示被指物是常量;
    如果出现在星号右边,表示指针自身是常量;
    如果出现在星号两边,表示被指物和指针两者都是常量;

2. primer C++中, 建议 从右往左读, 碰到变量名后,遇到const为常量,遇到 星号(*)为指针,
 这样:
 int * const p   读作  常量指针, 指针是常量,不可变;
 const int * p 或者 int const * p  读作 指针常量 , 指针所指物是常量,不可变;
 常量指针,指针常量,太容易搞混了,还是记作 const pointer, pointer to const吧,
 起码字数不一样,分得开。。。

      int x;
      int *       p1 = &x;  // non-const pointer to non-const int
      const int *       p2 = &x;  // non-const pointer to const int
      int * const p3 = &x;  // const pointer to non-const int
      const int * const p4 = &x;  // const pointer to const int 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值