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

前端 未结 5 1209
我在风中等你
我在风中等你 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:27

    It's not an optimization. An optimization is the transformation of a program into another program that has the same behaviour, but uses less resources. Your program does not have any defined behaviour, so you can't possibly apply any transformation to it that yields the same behaviour.

提交回复
热议问题