Consider the below code snippet:
int main()
{
const int i=3;
int *ptr;
ptr=const_cast(&i);
*ptr=5;
cout<<\"i= \"<&
I hade same problem, I added volatile, now it's modifying:
#include
using namespace std;
int main()
{
volatile const int a=5;
int *p = const_cast(&a);
*p=6;
cout<<"a="<
Output:
a=6
volatile tells compiler that the identifier can be modified (if not by this code then by some one else, so do not perform any optimization)