c模拟c++ const 转换

时间:2023-01-01 03:14:48

#include <stdio.h>

int main(){

const int constant = 21;
const int* const_p = &constant;
int* modifier = (int*)(const_p);

*modifier=10;
printf("%d \n",constant);
return 0;
}

结果是10

#include <iostream>
using namespace std;
int main(){

const int constant = 21;
const int* const_p = &constant;
int* modifier = (int*)(const_p);

*modifier=10;
cout<<constant<<endl;
return 0;
}

结果是21,这种模拟,对c++属于未定义操作~