+ 3

Mystery of const_cast

const int t = 9; const int *p = &t; *const_cast<int*>(p) = 999999; cout << p << " " << *p << endl; cout << &t << " " << t << endl; Why isn't "*p" the same value with "t"??

12th Mar 2018, 3:43 PM
Disvolviĝo;
Disvolviĝo; - avatar
9 Antworten
+ 3
hello.
13th Mar 2018, 2:42 PM
Raj Chhatrala
Raj Chhatrala - avatar
+ 3
give me some time I will explain you.
13th Mar 2018, 3:56 PM
Raj Chhatrala
Raj Chhatrala - avatar
+ 2
hellooo
13th Mar 2018, 3:50 PM
Disvolviĝo;
Disvolviĝo; - avatar
+ 2
Ok thank you!
13th Mar 2018, 3:58 PM
Disvolviĝo;
Disvolviĝo; - avatar
+ 2
const_cast is safe to use only with reference/pointer that point to not-const variables else result is undefined... I think that result is dependent from compiler internals and optimizations
14th Mar 2018, 2:05 PM
KrOW
KrOW - avatar
+ 1
what's going on.
13th Mar 2018, 3:51 PM
Raj Chhatrala
Raj Chhatrala - avatar
+ 1
I wanna know why the values of the '*p' and 't' are different!
13th Mar 2018, 3:55 PM
Disvolviĝo;
Disvolviĝo; - avatar
+ 1
Thank you. I am happy to know the reason!
14th Mar 2018, 2:08 PM
Disvolviĝo;
Disvolviĝo; - avatar
+ 1
👍👍👍
14th Mar 2018, 2:09 PM
KrOW
KrOW - avatar