对于下面的代码片段,
#include <iostream>
using namespace std;
void fun(const int *p)
{
int *q = const_cast<int *>(p);
*q = *q * 10;
cout<<"q: "<<q<<"\t Value: "<<*q<<endl;
}
int main()
{
const int a = 10;
const int *z = &a;
fun(z);
cout<<"z: "<<z<<"\t"<<"Address of a: "<<&a<<endl;
cout<<"value at z: "<<*z<<"\t\t value in a: "<<a<<endl;
}
产生的输出是
q: 0x7fff65910fcc Value: 100
z: 0x7fff65910fcc Address of a: 0x7fff65910fcc
value at z: 100 value in a: 10
为什么a的值不被修改,即使我试图修改它的乐趣()?
怎么来的地址和指针z是相同的,但值是不同的?
它是某种具有的const_cast未定义行为?