【发布时间】:2016-12-16 06:58:27
【问题描述】:
为什么我不能将const int *cp1 放在作业的右侧?请看这个样本
int x1 = 1;
int x2 = 2;
int *p1 = &x1;
int *p2 = &x2;
const int *cp1 = p1;
p2 = p1; // Compiles fine
p2 = cp1; //===> Complilation Error
为什么我会在指定位置收到错误消息?毕竟我不是想 更改一个常量值,我只是想使用一个常量值。
我在这里错过了什么吗?
【问题讨论】:
-
您不能直接将其剥离。如果您可以删除它并修改变量,那么 consts 的意义何在。
标签: c++ pointers constants variable-assignment