The reason you see 10 printed for ia is most likely a compiler optimization: it sees the const object, decides that it will not change, and replaces the last printout as follows:
cout<< 10 <<" "<<*ppa<<endl;
In other words, the generated code has a value of const , "baked in" binary format.
Discarding the constant of an object that was originally declared as const , and writing to this object is undefined behavior:
$ 5.2.11 / 7 - Note: depending on the type of object, a write operation through a pointer, lvalue, or a pointer to a data item obtained from const_cast, which discards const-qualifier68), can lead to undefined behavior (7.1.5.1).
Depending on the platform, const objects may be placed in a protected area of memory that you cannot write to. Working with const -ness in a type system can help compile your program, but you can see random results or even crash.
source share