const in c++
What if i use pointer to change directly on memory?
const int x = 5;
x = 10; // Compilation errorconst int x = 5;
int* ptr = const_cast<int*>(&x);
*ptr = 10; // Compiles, but is undefined behaviorint y = 5; const int* ptr = &y; // *ptr = 10; // Compilation error y = 10; // This is allowed
Last updated