The reason why you see 10
printed for ia
is most likely the compiler optimization: it sees a const
object, decides that it's not going to change, and replaces the last printout with this:
cout<< 10 <<" "<<*ppa<<endl;
In other words, the generated code has the value of the const
"baked into" the binary.
Casting away the const-ness of an object that has originally been declared as const
and writing to that object is undefined behavior:
$5.2.11/7 - Note: Depending on the type of the object, a write operation through the pointer, lvalue or pointer to data member resulting from a const_cast that casts away a const-qualifier68) may produce undefined behavior (7.1.5.1).
Depending on the platform, const
objects may be placed in a protected region of memory, to which you cannot write. Working around the const
-ness in the type system may help your program compile, but you may see random results or even crashes.