c++ 控制变化的const

const与基本数据类型

int x=3 //变量

const int x=3 //常量
x=4; //编译器会报错,常量不可改变
const 与指针类型
const int *p=NULL;等价于int const *p=NULL;

int* const p=NULL;

const int*const p=NULL; 等价于 int const * const p=NULL;


ex:
int x=3;
const int *p=&x; //const写在*的前面,修饰的是*p,所以*p不能变
p=&y;//正确
*p=4;//错误

int *const p1=&x;//const写在*的后面,修饰的是p,所有p不能变只能指向一个地址
p=&y;//错误


const与引用

int x=3;
const int &y=x;//y是x的别名,但是被const修饰了
x=10;//正确
y=20;//错误


ex:
const int x=3; x=5;//错误

int x=3;  const

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值