I am having problem with my const variable. When I create a non-constant reference to this variable, the problem arises is that the two variables have different values although the address remaining same.
Code:
#include <iostream.h>
#include <conio.h>
int main()
{
clrscr();
const int k = 4;
int& p = const_cast<int&>(k);
p++;
cout << "k = " << k << "\t&k = " << &k << endl
<< "p = " << p << "\t&p = " << &p << endl;
getch();
}
Code Output:
k = 10 &k = 0xbfffca88
p = 11 &p = 0xbfffca88
Which is wrong. A single address cannot store 2 different values, if I am not wrong then. Then what is wrong in my code?
Bookmarks