Getting the same value of the const variable even after changing it through const_cast

前端 未结 5 1213
我在风中等你
我在风中等你 2021-01-21 23:47

Consider the below code snippet:

int main()
{
    const int i=3;
    int *ptr;

    ptr=const_cast(&i);
    *ptr=5;

    cout<<\"i= \"<&         


        
5条回答
  •  终归单人心
    2021-01-22 00:42

    Most likely since it is a const int, the compiler is optimizing and directly replacing it with the value of i.

提交回复
热议问题