美文网首页C Primer Plus
const int *i,const int &i含义

const int *i,const int &i含义

作者: 桐桑入梦 | 来源:发表于2019-12-31 18:34 被阅读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

相关文章

网友评论

    本文标题:const int *i,const int &i含义

    本文链接:https://www.haomeiwen.com/subject/kcrioctx.html