C++中Const 修饰指针就近原则
来源:互联网 发布:英语复读机软件 编辑:程序博客网 时间:2024/05/15 17:16
转载地址:http://www.myexception.cn/cpp/1070329.html
C和C++中Const 修饰指针个人诀窍--就近原则
C、C++中Const修饰指针时总是记不住到底作用的是指针还是指针指向的变量内容:最近总结了一个绝招,可以绝对理清楚!那就是“就近原则”
所谓就近原则,意思就是const靠哪个近,就作用在那个上面!
比如:const靠int类型关键字近,那就是说修饰的是整形变量;
const靠指针变量名近,那就是说修饰该指针,说明它不是指针变量,而是指针常量;
但是有一个问题:既然说到是const修饰指针,那就一定有*,比如char const * pContent; 那这个const在*和变量名之间,到底靠谁近呢?
这里要解释说明的是:*不算,const靠谁近指的是类型(如int、char等)和变量名(如这里的pContent),不要算*
有了这一条就近原则,const问题迎刃而解!
1) const在前面
const int nValue; //nValue是const
const char *pContent; //*pContent是const, pContent可变
const (char *) pContent; //pContent是const,*pContent可变
char* const pContent; //pContent是const,*pContent可变
const char* const pContent; //pContent和*pContent都是const
2) const在后面,与上面的声明对等
int const nValue; // nValue是const
char const * pContent; // *pContent是const, pContent可变
(char *) const pContent; //pContent是const, *pContent可变
char* const pContent; // pContent是const, *pContent可变
char const* const pContent; // pContent和*pContent都是const
- C++中Const 修饰指针就近原则
- c语言中const修饰指针变量
- [c/c++]const修饰指针
- 就近原则—const关键字的使用
- 就近原则
- C:带有const修饰的指针解读
- C语言--const修饰指针解析
- C语言const关键字修饰指针
- C:带有const修饰的指针解读
- C/C++ const 修饰指针变量
- C++:带有const修饰的指针解读
- java-就近原则(方法重载中)
- C++中const修饰指针探讨
- C中指针const
- [C++] C++中const修饰指针,变量, 函数参数和函数返回值的用法总结
- const 修饰 指针
- const修饰指针
- 关于const修饰指针
- 竞速游戏任务系统设计
- C语言做图 书 信 息 管 理 系 统,大学的孩子们 福音在哪,这足够应付你们考试了
- 逻辑卷管理器--LVM
- OCP-1Z0-051-V9.02-132题
- netty的心跳检测实现
- C++中Const 修饰指针就近原则
- 自动收集和手工收集统计信息讲解
- 天堂门坏了上帝招标
- 介绍一个团队协同工作工具:Trello
- debian arm source list
- 打开windows的路由功能
- 在自己的WSASOCKET服务端/客户端中做了一个简单的解包程序处理粘包的问题
- iphone 线程总结— detachNewThreadSelector的使用
- android service 学习