Tekkaman

导航

 

如何理解*

  const char*, char const*, char*const的区别问题几乎是C++面试中每次都会有的题目。 事实上这个概念谁都有,只是三种声明方式非常相似很容易记混。Bjarne在他的The C++ Programming Language里面给出过一个助记的方法: 把一个声明从右向左读。 

  char * const cp; ( * 读成 pointer to
  cp is a const pointer to char 

  const char * p; 
  p is a pointer to const char; 

  char const * p; 
  同上因为C++里面没有const*的运算符,所以const只能属于前面的类型。 

  C++标准规定,const关键字放在类型或变量名之前等价的。

 1 char ** p1; 
 2 //    pointer to    pointer to    char 
 3 const char **p2;
 4 //    pointer to    pointer to const char 
 5 char * const * p3;
 6 //    pointer to const pointer to    char 
 7 const char * const * p4;
 8 //    pointer to const pointer to const char 
 9 char ** const p5;
10 // const pointer to    pointer to    char 
11 const char ** const p6;
12 // const pointer to    pointer to const char 
13 char * const * const p7;
14 // const pointer to const pointer to    char 
15 const char * const * const p8;
16 // const pointer to const pointer to const char

参考:http://blog.csdn.net/yingxunren/article/details/3968800

posted on 2014-02-11 21:41  Tekkaman  阅读(347)  评论(0编辑  收藏  举报