Why can you kind of cheat compiler this way:
const int a = 5;
*((int*)&a)=5; // VC/armcc does not complain
when above is “abridged” equivalent of this:
const int *ptr2const = &a;
int *ptr = ptr2const; // as expected error is raised here
*ptr = 5;
C-style casts allow you to cast away constness like in your example. In C++, you would normally use the new style casts such as
static_cast<>, which don’t allow you to cast away constness. Onlyconst_cast<>allows you to do that.