Const 的使用
来源:互联网 发布:多宝视网络弱视训练 编辑:程序博客网 时间:2024/04/29 13:33
- Const
- 非const指针不能指向const对象。因为通过指针可以间接修改指向的对象的值,这是const对象不允许的。所以,我们必须申明一个能指向常量的指针。
const int size=23
const int *pint=&size;
pint是一个指向常量的指针,不能通过pint修改size的值,但是,pint可以改为指向其它对象。
- const对象的指针只能赋给指向const对象的指针;但是指向const对象的指针可以被赋与非const对象。
int total=20;
const int *pint =&total;
total是非const,但可以赋给指向const对象的指针。不过,企图通过pint修改total的值是非法的,因为pint是指向const的提针,不应该修改其指向的对象的内容。
- const指针不能再指向别的对象,但可以修改指向的值。因为,指针本身是const,并不是所指的内容是const。
int total=100;
int size=20;
int *const pint=&total;
pint=&size;//Error
- 指向const的const指针,无论是修改指向的对象,或者修改所指对象的值都是非法的。
int total=100;
int size=10;
const int *const pint=&total;
*pint=99;//Error,按pint的定义,它指向的对象是const,不允许修改
pint=&size;//Error,pint是常指针,不能再指向其它对象
- const的使用,const char * const foo(char const * const str) const;什么意思?
- Const 的使用
- const的使用
- const的使用
- Const的使用
- 关于const的使用
- const的使用
- const volatile的使用
- const 的使用
- const的使用
- 使用const的好处
- Const 的使用
- const关键字的使用
- const的使用
- const的使用
- const 的使用
- 使用Const的理由
- const的使用
- 术语Ajax
- 转贴:兴宁风味小吃
- 好天气!
- 我们一起来估算中国有多少人在奋斗
- C++中的虚函数(virtual function)
- Const 的使用
- 感觉时间不够用啊!
- Js作下拉菜单跳转
- local.conf范例
- Windows Server 2003 新增命令行工具
- Windows Server 2003下简单的Mail服务器
- 北京出现嗜考症学生 一天不学习心里就发慌
- Windows Server 2003 + IIS6.0 环境下安装PHP
- 博士生为摆脱书呆子形象 集体学习跳热辣街舞