I could not understand why in below code snippet reference and its associated variable are giving different results.
const int x = 10;
const int &ptr = x;
int& y = const_cast<int&>(ptr);
y = 19;
std::cout << "x = " << x << " ptr=" << ptr << " y=" << y << std::endl;
Output:
x=10 ptr=19 y=19
As per my understanding references are just alias for variable so why ptr
is 19
and x
is 10
? Is this something to do with const
or const_cast
?