const修饰符可以把对象转变成常数对象,什么意思呢?
意思就是说利用const进行修饰的变量的值在程序的任意位置将不能再被修改,就如同常数一样使用!
使用方法是:
const int a=1;//这里定义了一个int类型的const常数变量a;
但就于指针来说const仍然是起作用的,以下有两点要十分注意,因为下面的两个问题很容易混淆!
我们来看一个如下的例子:
![None.gif](http://www.cppblog.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://www.cppblog.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://www.cppblog.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://www.cppblog.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockStart.gif](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockEnd.gif](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
上面的代码中最重要的一句是 const int *pi
这句从右向座读作:pi是一个指向int类型的,被定义成const的对象的指针;
这样的一种声明方式的作用是可以修改pi这个指针所指向的内存地址却不能修改指向对象的值;
如果你在代码后加上*pi=10;这样的赋值操作是不被允许编译的!
好,看了上面的两个例子你对const有了一个基本的认识了,那么我们接下来看一个很容易混淆的用法!
请看如下的代码:
![None.gif](http://www.cppblog.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://www.cppblog.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://www.cppblog.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://www.cppblog.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockStart.gif](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockEnd.gif](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
上面的代码中最重要的一句是 const int *const pi
这句从右向座读作:pi是一个指向int类型对象的const指针;
这样的一种声明方式的作用是你既不可以修改pi所指向对象的内存地址也不能利用指针的解引用方式修改对象的值,也就是用*pi=10这样的方式;
所以你如果在最后加上*pi=20,想试图通过这样的方式修改对象a的值是不被允许编译的!
结合上面的两点所说,把代码修改成如下形式后就可以必然在程序的任意的地方修改对象a的值或者是指针pi的地址了,下面的这种写法常被用语涵数的形式参数,这样可以保证对象不会在涵数内被改变值!
![None.gif](http://www.cppblog.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://www.cppblog.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://www.cppblog.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://www.cppblog.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockStart.gif](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockEnd.gif](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://www.cppblog.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)