一、有符号数与无符号数
有符号数和无符号数运算自动变成无符号数
二、指针与引用
1,某个值的引用无法改变,即是说引用只能引用一个对象
2.int &*p 错误,因为引用不是对象;int *&p正确
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.