const volatile 和 const 问题的探究
请来看这么一个例子:
C++代码:
const int n=100;
int*nn=const_cast<int*>(&n);
*nn=111;
cout<<n<<endl;//输出1
cout<<*nn<<endl;//输出2
cout<<&n<<endl;//输出3
cout<<nn<<endl;//输出4
-------------------
输出1:100
输出2: 111
其实从规则上,我知道这种现象是正常的。应该这个n没有volatile修饰。
于是我猜想,产生这种结果的可能是n的地址和nn的值可能不等。
但是输出3和输出4的值却是相等的。
再看一个例子:
const volatile int n=100;
int*nn=const_cast<int*>(&n);
*nn=111;
cout<<n<<endl;//输出1
cout<<*nn<<endl;//输出2
cout<<&n<<endl;//输出3
cout<<nn<<endl;//输出4
-----------------
输出1:111
输出2:111
这没什么好说的了。
问题是输出3的结果是1.
他的地址是1.这如何解释。
综上,我有两个问题:
问题1:为什么例子1里面的输出3和4的地址一样,输出1,2的结果却不一样。
问题2:为什么例子2中,输出3的值是1.