C Primer Plus

const int *i,const int &i含义

2019-12-31  本文已影响0人  桐桑入梦
const int *i 是指向常量的指针,指针指向一个常量,无需初始化,指针本身可以改变,但是指针指向的值不能改变。
如:
const int x=10;
const int *p1=&x;
p1++;//ok
(*p1)++;//error
const int &i是指向常量的引用,使用时必须初始化,而且初始化后,引用值不可以改变,引用的常量也不能改变。
如:
const int &p2;//error
const int &p2=x;//ok
const int y=20;
p2=y;//error
p2++;//error
上一篇 下一篇

猜你喜欢

热点阅读