const T、const T*、T const、const T&、const T& 的区别
T是指一种数据类型,可以是基本数据类型,也可以是自己定义的类型,单独的一个const是指一个常量。
const T
定义一个常量,声明的同时必须进行初始化。一旦声明,这个值也将不能被改变。
const T*
指向常量的指针,不能改变所指向对象的值。
1 const int i = 5;
2 const int i2 = 10;
3 const int* pInt = &i; //正确,指向一个const int对象,即i的地址
4 //*pInt = 10; //错误,不能改变其所指缶的对象
5 pInt = &i2; //正确,可以改变pInt指针本身的值,此时pInt指向的是i2的地址
6 const int* p2 = new int(8); //正确,指向一个new出来的对象的地址
7 delete p2; //正确
8 //int* pInt = &i; //错误,i是const int类型,类型不匹配,不能将const int * 初始化为int *
9 int nValue = 15;
10 const int * pConstInt = &nValue;//正确,可以把int *赋给const int *,但是pConstInt不能改变其所指向对象的值,即nValue
11 *pConstInt = 40; //错误,不能改变其所指向对象的值
const T* 与T* const的区别
指针本身也是一种对象,把指针定义为常量就是常量指针,就是T* const类型,也可以写成T *const,声明时必须初始化华。
1 int nValue = 10;
2 int* const p = &nValue;
3 int *const p2 = &nValue;
4 //const int* 指针指向的对象不可以改变,但指针本身的值可以改变;
5 //int* const 指针本身的值不可改变,但其指向的对象可以改变。
6 int nValue1 = 10;
7 int nValue2 = 20;
8 int* const constPoint = &nValue1;
9 //constPoint = & nValue2; //错误,不能改变constPoint本身的值
10 *constPoint = 40; //正确,可以改变constPoint所指向的对象,此时nValue1 = 40
11
12
13 const int nConstValue1 = 5;
14 const int nConstValue2 = 15;
15 const int* pPoint = &nConstValue1;
16 //*pPoint = 55; //错误,不能改变pPoint所指向对象的值
17 pPoint = &nConstValue2; //正确,可以改变pPoint指针本身的值,
18 //此时pPoint邦定的是nConstValue2对象,即pPoint为nConstValue2的地址
const T* const
是一个指向常量对象的常量指针,即不可以改变指针本身的值,也不可以改变指针指向的对象。
1 const int nConstValue1 = 5;
2 const int nConstValue2 = 15;
3 const int* const pPoint = &nConstValue1;
4 //*pPoint = 55; //错误,不能改变pPoint所指向对象的值
5 //pPoint = &nConstValue2; //错误,不能改变pPoint本身的值
const T&
对常量的应用,又称常量引用,常量引用不能修改其绑定的对象。
1 int i = 5;
2 const int constInt = 10;
3 const int& rConstInt = constInt; //正确,引用及邦定的值都是常量
4 rConstInt = 5; //错误,不能改变引用所指向的对象
允许为一个常量引用邦定一个非常量对象、字面值,甚至是表达式;引用的类型与引用所指向的类型必须一致。
1 int i = 5;
2 int& rInt = i; //正确,int的引用
3 const int constInt = 10;
4 const int& rConstInt = constInt; //正确,引用及邦定的值都是常量
5 const int& rConstInt2 = rInt; //正确,用rInt邦定的对象进行赋值
6 rInt = 30; //这时,rConstInt2、rInt、i的值都为30
7 //rConstInt2 = 30; //错误,rConstInt2是常量引用,rConstInt2本身不能改变所指向的对象
8
9
10 int i2 = 15;
11 const int& rConstInt3 = i2; //正确,用非常量的对象为其赋值
12 const int& rConstInt4 = i + i2; //正确,用表达式为其赋值,值为45
13 i = 20; //此时i=20, rInt = 20, rConstInt4 = 45,说明rConstInt4邦定的是i + i2的临时变量
14 const int& rConstInt5 = 50; //正解,用一个常量值为其赋值
const T*&与T *const&
指向常量对象的指针的引用,这可以分两步来理解:
1.const T*是指向常量的指针;
2.const T*&指向常量的指针的引用。
1 const int nConstValue = 1; //常量对象
2 const int nConstValue2 = 2; //常量对象
3 const int* pConstValue = &nConstValue; //指向常量对象的指针
4 const int* pConstValue2 = &nConstValue2; //指向常量对象的指针
5 const int*& rpConstValue = pConstValue; //指向常量对象的指针的引用
6 //*rpConstValue = 10; //错误,rpConstValue指向的是常量对象,常量对象的值不可改变
7 rpConstValue = pConstValue2; //正确,此时pConstValue的值等于pConstValue2
8 //指向常量对象的指针本身是对象,引用可以改变邦定对象的值
9
10 int nValue = 5;
11 int nValue2 = 10;
12 int *const constPoint = &nValue; //常量指针
13 int *const constPoint2 = &nValue2; //常量指针
14 int *const &rpConstPoint = constPoint; //对常量指针的引用,邦定constPoint
15 //rpConstPoint = constPoint2; //错误,constPoint是常量指针,指针本身的值不可改变
16 *rpConstPoint = 20; //正确,指针指向的对象可以改变