【发布时间】:2015-10-16 07:27:26
【问题描述】:
int main() {
const int i =10;
int *j = const_cast<int*>(&i);
cout<<endl<<"address of i "<<&i;
cout<<endl<<"value of j "<<j;
(*j)++;
cout<<endl<<"value of *j "<<*j;
cout<<endl<<"value of i "<<i;
// If Not use Const //
int k = 10;
int *p = &k;
cout<<endl<<"address of k "<<&i;
cout<<endl<<"address of p "<<&p;
(*p)++;
cout<<endl<<"value of *p "<<*p;
cout<<endl<<"value of k "<<k<<endl;
}
输出是:
i0xbf8267d0的地址j0xbf8267d0 的值*j11 的值i10 的值k0xbf8267d0的地址p0xbf8267c8的地址*p11 的值k11 的值
有人可以在输出的第 3 行和第 4 行解释原因吗
*j is 11 和 i is 10。
为什么i 也不是 11?
【问题讨论】:
-
重写 const 变量是未定义的行为。