首页 > 其他 > 详细

the difference between const int *, int * const, int const *

时间:2016-12-05 16:57:06      阅读:241      评论:0      收藏:0      [点我收藏+]

  Some people may be confused about the sequence of const and * on declaration in C++/C, me too.

  Now I think we can distinguish them by this way:

  1.only noticing the position of const to *, and we can find that the following statements are same:    

const int * foo_int;
int const * foo_int_;  //same.

  2.regarding const as a postpositive attributes,and you will know the content which is const.

  

int foo = 6;
int foo_ = 7;

//the foo_int is const, but the pointer to foo_int can be chaged.
int const * foo_int = &foo;

//illegal, the value cannot be chaged
//*foo_int = 7

//okay!
foo_int = &foo_;

//the pointer to int is const, but foo_int_ can be chaged.
int * const foo_int_ = &foo_int;

//illegal, the pointer cannot be changed.
//foo_int = &foo_int_;
//even if the new pointer is same, it‘s illegal
//foo_int = &foo_int;

//okay
*foo_int = 8;

 

the difference between const int *, int * const, int const *

原文:http://www.cnblogs.com/maikaze/p/6134361.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!