As much as I know, const_cast may be used to cast away (remove) constness or volatility. But I could not understand the execution of the code below.
#include <iostream>
int main()
{
const int i = 20;
int *p = const_cast<int*>(&i);
std::cout << "i:" << i << " *p:" << *p << " p:" << p << " &i:" << &i << std::endl;
(*p)++;
std::cout << "i:" << i << " *p:" << *p << " p:" << p << " &i:" << &i << std::endl;
return 0;
}
When I tried to run the above code, following result was obtained:
i:20 *p:20 p:0x7ffc579d18e4 &i:0x7ffc579d18e4
i:20 *p:21 p:0x7ffc579d18e4 &i:0x7ffc579d18e4
Can anyone explain me the output. I am new to C++ and trying to understand different casting operation. Thanks in advance