Let's see the example first.
#include <iostream>
int main()
{
const int constant = 1;
const int* const_p = &constant;
int* modifier = const_cast<int*>(const_p);
*modifier = 100;
std::cout << "constant: " << constant << ", *const_p=" << *const_p;
//Output: constant: 1, *const_p=100
return 0;
}
I don't know how it achieved in the memory architecture. It seems that the compiler have occupied extra memory space in the stack so that we can keep track of the "original" constant
whose value is 1
, and a new memory location in the stack whose value is 100
. Is it? So will const_cast
indeed consume extra memory as a beginner might not first expect?