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
网友评论