C++ const 和指针类型

//以下2种情况表示的意思相同
const int *p=NULL;
int const *p=NULL;
//以上情况与以下情况不同
int *const p=NULL;

//以下2种情况表示的意思相同
const int *const p=NULL;
int const *const p=NULL;
int x=3; 
const int *p=&x;//const修饰在*p*p指向x的地址
p=&y;//正确
*p=4;//错误,通过*p改变x的值是错误的!
变量名字 存储地址 存储内容
x &x 3
p &p &x

再来一个例子:

int x=3; 
int *const p=&x;//const修饰在p,意味着*p只能唯一指向x的地址
p=&y;//错误,p又指向了y的地址

const 和引用的关系

int x=2;
const int &y=x;//引用
//x=10;正确   //y=20;错误

posted on 2015-10-31 22:10  进击的小秦  阅读(109)  评论(0编辑  收藏  举报