const 关键字 :const T、const T*、T *const、const T&、const T*& 的区别

 

const 关键字

C++中的const关键字的用法非常灵活,而使用const将大大改善程序的健壮性,本人根据各方面查到的资料进行总结如下,期望朋友们有所帮助。

Const C++中常用的类型修饰符,常类型是指使用类型修饰符const说明的类型,常类型的变量或对象的值是不能被更新的。

 

可以使用 const 前缀声明指定类型的常量

const type variable = value;

具体请看下面的实例:

实例

#include <iostream>
using namespace std;
 
int main()
{
   const int  LENGTH = 10;
   const int  WIDTH  = 5;
   const char NEWLINE = '\n';
   int area;  
   
   area = LENGTH * WIDTH;
   cout << area;
   cout << NEWLINE;
   return 0;
}

当上面的代码被编译和执行时,它会产生下列结果:

50

请注意,把常量定义为大写字母形式,是一个很好的编程实践。


const T、const T*、T *const、const T&、const T*& 的区别

const T

定义一个常量,声明的同时必须进行初始化。一旦声明,这个值将不能被改变。

 
  1. int i = 5;

  2. const int constInt = 10; //正确

  3. const int constInt2 = i; //正确

  4. constInt = 20; //错误,常量值不可被改变

  5. const int constInt3; //错误,未被初始化


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 int* 与int* const的区别

指针本身就是一种对象,把指针定义为常量就是常量指针,也就是int* const的类型,也可以写成int *const,声明时必须初始化。

 
  1. int nValue = 10;

  2. int* const p = &nValue;

  3. int *const p2 = &nValue;

  4. const int* 指针指向的对象不可以改变,但指针本身的值可以改变;int* const 指针本身的值不可改变,但其指向的对象可以改变。

  5. int nValue1 = 10;

  6. int nValue2 = 20;

  7. int* const constPoint = &nValue1;

  8. //constPoint = & nValue2; //错误,不能改变constPoint本身的值

  9. *constPoint = 40; //正确,可以改变constPoint所指向的对象,此时nValue1 = 40

  10.  
  11.  
  12. const int nConstValue1 = 5;

  13. const int nConstValue2 = 15;

  14. const int* pPoint = &nConstValue1;

  15. //*pPoint = 55; //错误,不能改变pPoint所指向对象的值

  16. pPoint = &nConstValue2; //正确,可以改变pPoint指针本身的值,此时pPoint邦定的是nConstValue2对象,即pPoint为nConstValue2的地址1


const int* 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&

对常量(const)的引用,又称为常量引用,常量引用不能修改其邦定的对象。

 
  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; //正确,指针指向的对象可以改变

Const的使用

1定义常量
(1)const修饰变量,以下两种定义形式在本质上是一样的。它的含义是:const修饰的类型为TYPE的变量value是不可变的。

 TYPE const ValueName = value; 
     const TYPE ValueName = value;


(2)const改为外部连接,作用于扩大至全局,编译时会分配内存,并且可以不进行初始化,仅仅作为声明,编译器认为在程序其他地方进行了定义.

     extend const int ValueName = value;

2、指使用CONST
(1)指针本身是常量不可变
     char* const pContent; 

(2)指针所指向的内容是常量不可变
     const char *pContent; 

(3)两者都不可变
      const char* const pContent; 

(4)还有其中区别方法,沿着*号划一条线:
如果const位于*的左侧,则const就是用来修饰指针所指向的变量,即指针指向为常量;
如果const位于*的右侧,const就是修饰指针本身,即指针本身是常量。

 

 

 

部分转载自:

https://blog.csdn.net/Eric_Jo/article/details/4138548

https://blog.csdn.net/hzy925/article/details/78414461

猜你喜欢

转载自blog.csdn.net/weixin_42346564/article/details/81478482