区分const指针
2015-07-05 23:37
183 查看
Read it backwards...int*- pointer to int int const *- pointer to const int int * const- const pointer to int int const * const- const pointer to const int Now the first const can be on either side of the type so: const int *== int const * const int * const== int const * const If you want to go really crazy you can do things like this: int **- pointer to pointer to int int ** const- a const pointer to a pointer to an int int * const *- a pointer to a const pointer to an int int const **- a pointer to a pointer to a const int int * const * const- a const pointer to a const pointer to an int ... And to make sure we are clear on the meaning of const const int* foo; int *const bar; //note, you actually need to set the pointer //here because you can't change it later ;) foois a variable pointer to a constant int. This lets you change what you point to but not the value that you point to. Most often this is seen with cstrings where you have a pointer to a const char. You may change which string you point to but you can't change the content of these strings. This is important when the string itself is in the data segment of a program and shouldn't be changed. baris a const or fixed pointer to a value that can be changed. This is like a reference without the extra syntactic sugar. Because of this fact, usually you would use a reference where you would use a T* constpointer unless you need to allow null pointers. |
相关文章推荐
- Codeforces 238E. Meeting Her 图论+记忆化搜索
- HDU4630:No Pain No Game(线段树)
- 快速傅里叶变换用于长整数相乘
- 2天驾驭DIV+CSS (技巧篇)(转)
- AngularJS自定义表单控件
- LR11.5 安卓模拟器性能测试
- 两个栈来实现一个队列的C++代码(某公司社会早笔试题)
- 2015070510 - 挑战失败
- mac下安装、配置nexus,构建maven私服
- IIR递归高斯滤波总结
- 签到领金币
- Lua53
- 管理定律-管人用人育人留人之道
- HDU4622:Reincarnation(后缀数组,求区间内不同子串的个数)
- 2015070509 - 看书效率
- 2015070508 - EffactiveJava笔记 - 第13条 使类和成员的可访问性最小(2)
- ZooKeeper入门
- 调试 之gdb thread命令 与 ltrace/strace
- OMSA在Windows服务器上安装部署(服务器监控,RAID在线操作)
- JAVA基础-程序的基本结构、函数与数组