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