C++中const的详细解释
来源:互联网 发布:linux查看动态库依赖 编辑:程序博客网 时间:2024/05/16 04:46
原文网址:http://stackoverflow.com/questions/1143262/what-is-the-difference-between-const-int-const-int-const-int-const
Read it backwards...
int*
- pointer to intint const *
- pointer to const intint * const
- const pointer to intint 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 intint ** const
- a const pointer to a pointer to an intint * const *
- a pointer to a const pointer to an intint const **
- a pointer to a pointer to a const intint * 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 ;)
foo
is a variable pointer to a constant int. That is 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 changed the strings content. This is important when the string itself is in the data segment of a program and shouldn't be changed.
bar
is a const or fixed pointer to a value that can be changed. This is like a reference with out the extra syntactic sugar. Because of this fact, usually you would use a reference where you would use a T* const
pointer unless you need to allow null pointers.
=================================================================
typedef char *ASTRING;const ASTRING astring;
The type of astring
is char * const
, not const char *
. This is one reason I always tend to put const to the right of the type, and never at the start.
http://duramecho.com/ComputerInformation/WhyHowCppConst.html,const详解
http://www.cprogramming.com/tutorial/const_correctness.html,专家博客解释const
- C++中const的详细解释
- c++中const详细解释
- c++中const详细解释
- c++中const详细解释
- c++中const详细解释
- c++语言中const关键字的详细使用解释
- C语言中Valtile,Const,extern的解释
- C语言中const的详细用法及声明规则
- 【精华c++】static 和 const的解释
- C/C++中堆栈区别详细解释
- C++中static 和const的解释
- 关于C C++的const详细讲解
- 解释此函数原型中三个const的作用const int* computeResult( const int& var ) const。
- C/C++中函数的调用约定(__cdecl, __stdcall)以及extern, extern “C"的详细解释
- 【C/C++】static和const的比较和解释
- C++中const的用法详细总结
- C语言中auto,register,static,const,volatile的区别详细解析
- C语言中auto,register,static,const,volatile的区别详细解析
- Xen源代码分析(二)——trampoline.s
- winfrom开发小技巧三则
- 一个简单的数组排序程序
- oracle hint
- php学习 第四十九节
- C++中const的详细解释
- MsSQL获取所有表名及字段名
- Spring集成Xfire
- 动词记忆(1):ate,ify,ish后缀常用词
- QT学习笔记-QMessageBox
- Xen源代码分析(三)——x86_32.s
- 十八年开发经历小结
- ruby: 嵌入字符串、% 呈现、ruby文件读写ruby way之IO之一
- php学习 第五十节