const_cast: 转换 const 和 const volatile 类型之后的异同 (还有个疑问)

Code:

const int a = 1; cout << "a = " << a << "; &a = " << &a << endl; int * p = const_cast<int*>(&a); *p = 2; cout << "a = " << a << "; &a = " << &a << endl; cout << "*p = " << *p << "; p =" << p << endl << endl; const volatile int b = 50; cout << "b = " << b << "; &b = " << &b << endl; int *p2 = const_cast<int *>(&b); *p2 = 51; cout << "b = " << b << "; &b = " << &b << endl; cout << "*p2 = " << *p2 << "; p2 = " << p2 << endl;

 

output:

a = 1; &a = 0020FAD0 a = 1; &a = 0020FAD0 //a 仍然为一,因为读了寄存器的值,没刷新 *p = 2; p =0020FAD0 b = 50; &b = 1 //? &b 为什么是 1 阿? b = 51; &b = 1 *p2 = 51; p2 = 0020FAB8

你可能感兴趣的:(output)