chapter 2 指针与引用

一、有符号数与无符号数

有符号数和无符号数运算自动变成无符号数

二、指针与引用

1,某个值的引用无法改变,即是说引用只能引用一个对象
2.int &*p 错误,因为引用不是对象;int *&p正确

3. int i = 42;
   const int &r1 = i; // we can bind a const int& to a plain int object
   r1 = 10//错误,不能改值
   const int &r2 = 42; // ok: r1 is a reference to const
   #### int &r2 = 42 ;//error 

   const int &r3 = r1 * 2; // ok: r3 is a reference to const
   int &r4 = r1 * 2; // error: r4是一个普通的非常量引用,
    const int &r5 = i;//但是可以这样 具体解释:
因为当const int &r5 = i;时,编译器实际上是这样的
     const int a = i;
     const int &r5 = a;
所以实际上r5绑定的是a而不是,i;如果没有const,程序员希望可以通过修改r5改变i;可是r5绑定的是a,所以没用,所以c++规定这样使用非法。
4.区别 :const int *p 指向int型常量的指针,从右向左读;int *const p 常量指针指向int
5.自身是const的指针是顶层const,指向const的是底层const
6.常量表达式是指值不会改变并且在编译过程就能得到计算结果的表达式
int staff_size = 27; // staff_size is not a constant expression
const int sz = get_size(); // sz is not a constant expression,因为在编译过程中不能得到计算结果。
7.
typedef char *pstring;
const pstring cstr = 0; // cstr is a constant pointer to char指向char的常量指针,
const pstring *ps; // ps is a pointer to a constant pointer to char
我的理解这个时候pstring已经是一个char类型的指针,用const修饰它。所以是一个常量指针
The base type in these declarations is const pstring. As usual, a const that
appears in the base type modifies the given type. The type of pstring is “pointer to
char.” So, const pstring is a constant pointer to char—not a pointer to const
char.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值