Advertisement
karlicoss

const_cast wtf

Jun 23rd, 2011
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.52 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     const int a = 10;
  8.     int* pa = const_cast<int*>(&a);
  9.     cout << "a adress is " << &a << endl << "pa is " << pa << endl;//здесь адреса совпадают, что вполне логично
  10.     *pa = 5;//казалось бы, теперь a тоже должно быть равно 5
  11.     cout << "a is " << a << endl << "*pa is " << *pa << endl;//а здесь выводит a = 10, *pa = 5. КАК? у них же 1 адрес.
  12.     return 0;
  13. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement