const 作用
转载自:http://www.cnblogs.com/xudong-bupt/p/3509567.html
1.const 修饰成员变量

1 #include<iostream>
2 using namespace std;
3 int main(){
4 int a1=3; ///non-const data
5 const int a2=a1; ///const data
6
7 int * a3 = &a1; ///non-const data,non-const pointer
8 const int * a4 = &a1; ///const data,non-const pointer
9 int * const a5 = &a1; ///non-const data,const pointer
10 int const * const a6 = &a1; ///const data,const pointer
11 const int * const a7 = &a1; ///const data,const pointer
12
13 return 0;
14 }

const修饰指针变量时:
(1)只有一个const,如果const位于*左侧,表示指针所指数据是常量,不能通过解引用修改该数据;指针本身是变量,可以指向其他的内存单元。
(2)只有一个const,如果const位于*右侧,表示指针本身是常量,不能指向其他内存地址;指针所指的数据可以通过解引用修改。
(3)两个const,*左右各一个,表示指针和指针所指数据都不能修改。
2.const修饰函数参数
传递过来的参数在函数内不可以改变,与上面修饰变量时的性质一样。
void testModifyConst(const int _x) {
_x=5; ///编译出错
}
3.const修饰成员函数
(1)const修饰的成员函数不能修改任何的成员变量(mutable修饰的变量除外)
(2)const成员函数不能调用非onst成员函数,因为非const成员函数可以会修改成员变量

1 #include <iostream>
2 using namespace std;
3 class Point{
4 public :
5 Point(int _x):x(_x){}
6
7 void testConstFunction(int _x) const{
8
9 ///错误,在const成员函数中,不能修改任何类成员变量
10 x=_x;
11
12 ///错误,const成员函数不能调用非onst成员函数,因为非const成员函数可以会修改成员变量
13 modify_x(_x);
14 }
15
16 void modify_x(int _x){
17 x=_x;
18 }
19
20 int x;
21 };

4.const修饰函数返回值
(1)指针传递
如果返回const data,non-const pointer,返回值也必须赋给const data,non-const pointer。因为指针指向的数据是常量不能修改。

1 const int * mallocA(){ ///const data,non-const pointer
2 int *a=new int(2);
3 return a;
4 }
5
6 int main()
7 {
8 const int *a = mallocA();
9 ///int *b = mallocA(); ///编译错误
10 return 0;
11 }

(2)值传递
如果函数返回值采用“值传递方式”,由于函数会把返回值复制到外部临时的存储单元中,加const 修饰没有任何价值。所以,对于值传递来说,加const没有太多意义。
所以:
不要把函数int GetInt(void) 写成const int GetInt(void)。
不要把函数A GetA(void) 写成const A GetA(void),其中A 为用户自定义的数据类型。
在编程中要尽可能多的使用const,这样可以获得编译器的帮助,以便写出健壮性的代码。
const 作用的更多相关文章
- C++ 中const作用
一.对const与#define的特点及区别的理解 #define只是用来做文本替换的,#define常量的生命周期止于编译期,它存在于程序的代码段,在实际程序中它只是一个常数,一个命令中的参数,并没 ...
- const作用
const有以下几个作用: 1. 定义const常量,具有不可变性.eg. const int MAX = 100; int Array[MAX]; 2. 进行类型检查,使编译器对处理内容有更多的了 ...
- C语言关键字const作用及其应用
只要学过C语言的,都有知道const这个关键字,知道是用来定义常量的,如果一个变量被const修饰,那么它的值就不能再被改变,那么还有什么其他作用呢? 一.const常用作用 1.修饰局部变量 con ...
- C语言中Static和Const关键字的的作用
程序的局部变量存在于(堆栈)中,全局变量存在于(静态区 )中,动态申请数据存在于( 堆)中. 1.作用于变量: 用static声明局部变量-------局部变量指在代码块{}内部定义的变量,只在代码块 ...
- const ;static;extern的使用与作用
const /** const :常量 const ...
- C语言中Static和Const关键字的的作用 -- 转
static作用:“改变生命周期” 或者 “改变作用域” 程序的局部变量存在于(堆栈)中,全局变量存在于(静态区 )中,动态申请数据存在于( 堆)中. 1.作用于变量: 用static声明局部变量-- ...
- const,static,extern 简介
const,static,extern 简介 一.const与宏的区别: const简介:之前常用的字符串常量,一般是抽成宏,但是苹果不推荐我们抽成宏,推荐我们使用const常量. 执行时刻:宏是预编 ...
- C++中const的全面总结
C++中的const关键字的用法非常灵活,而使用const将大大改善程序的健壮性,本人根据各方面查到的资料进行总结如下,期望对朋友们有所帮助. Const 是C++中常用的类型修饰符,常类型是指使用类 ...
- 李洪强iOS经典面试题155 - const,static,extern详解(面试必备)
李洪强iOS经典面试题155 - const,static,extern详解(面试必备) 一.const与宏的区别(面试题): const简介:之前常用的字符串常量,一般是抽成宏,但是苹果不推荐我们抽 ...
随机推荐
- android stadio svn 使用技巧
有时候有这样的需求: 就是我一次要改很多的需求,然后代码要分开提交,那么怎么办? 提交的时候一个一个的点开看? 比如:这次改的还没有提上去,又来了一个需求,怎么区分呢 新建一个active的变化列表 ...
- android studio 首字母提示 设置 大小写敏感
在使用Android studo 编写程序时, 刚开始,关键字提示 首字母 设置了 大小写敏感,小写字母只能提示小写字母开头的,大写字母只能提示大写字母开始的,比较麻烦,在网上搜了下,解决办法如下: ...
- TP5 中出现 No input file specified
之前用php5.4 更新至php7之后原tp5项目出现 No input file specified 修改方法: 打开public目录下的.htaccess文件,把:RewriteRule ^(.* ...
- python考点
Python考点 1.Python类继承,内存管理(阿里) 答:内存管理机制包括:引用计数机制,垃圾回收机制,内存池机制 a = 1,1就是对象,a就是引用,引用a指向对象1. 2.Python装饰器 ...
- Vbs 测试程序一
转载请注明出处 有点小恶意哦!慎重测试 'This procedure is written in SeChaos, only for entertainment, not malicious com ...
- spring 笔记2:Spring MVC : Did not find handler method for 问题的解决
日志显示为: Looking up handler method for path /***Did not find handler method for [/***]No mapping found ...
- node.js和npm的安装
1.进入node.js官方网站,下载和系统对应的版本,我下载的是这个最新版本:node-v8.2.1-x64.msi https://nodejs.org/en/download/ 2.windows ...
- python学习总结 --函数基础
函数基础 ### 函数简介 - 定义:具有特定功能的一段代码 - 优点: - 可以减少代码的重复书写 - 可以将功能的实现着和使用者分开,可以提高开发效率 - 分类: - 库函数:print.inpu ...
- hexo 添加标签
--- title: title #文章標題 date: 2016-06-01 23:47:44 #文章生成時間 categories: "Hexo教程" #文章分類目錄 可以省略 ...
- vue & lifecycle methods & this bug & ES6 Arrow function & this bind bug
vue & lifecycle methods & this bug ES6 Arrow function & this bind bug bad fetchTableData ...