h****b 发帖数: 157 | 1 const int i =5;
(*const_cast(&i)) = 6;
cout<
为啥i打印的还是5?是不是complier自动把i替换成5所以不能改?那const_cast在什么
时候能改变
变量值 ?
谢谢 | t****t 发帖数: 6806 | 2 if i itself is const int, then any attempt to modify it is undefined. in fac
t, compiler could even optimize the value out when you reference i (use a co
nst instead).
const_cast can only be used to modify something that is not const, but is ca
st (temporarily) to const, e.g.
int i=5;
const int *cpi=&i;
//*cpi=6; //this is of course wrong
(*const_cast(cpi))=6; //correct
【在 h****b 的大作中提到】 : const int i =5; : (*const_cast(&i)) = 6; : cout<: 为啥i打印的还是5?是不是complier自动把i替换成5所以不能改?那const_cast在什么 : 时候能改变 : 变量值 ? : 谢谢
| h****b 发帖数: 157 | |
|