const-cast

Is casting a pointer to const pointer and cast back to the original type undefined?

吃可爱长大的小学妹 提交于 2021-01-29 02:24:16
问题 I know casting a const pointer to non-const type might be undefined behavior, but what if the pointer is originally not const? int i = 0; int * pi = &i; const int * const_pi = const_cast<const int*>(pi); int * non_const_pi = const_cast<int*>(const_pi); *non_const_pi = 0; *non_const_pi = 1; int j = *non_const_pi; Is there's any undefined behavior? If any, where do they happen? May the compiler assume that non_const_pi is casted from a const pointer and perform no modification? 回答1: I know

Is casting a pointer to const pointer and cast back to the original type undefined?

China☆狼群 提交于 2021-01-29 02:21:30
问题 I know casting a const pointer to non-const type might be undefined behavior, but what if the pointer is originally not const? int i = 0; int * pi = &i; const int * const_pi = const_cast<const int*>(pi); int * non_const_pi = const_cast<int*>(const_pi); *non_const_pi = 0; *non_const_pi = 1; int j = *non_const_pi; Is there's any undefined behavior? If any, where do they happen? May the compiler assume that non_const_pi is casted from a const pointer and perform no modification? 回答1: I know

Why does const_cast remove constness for a pointer but not for a pointer to a const?

自作多情 提交于 2020-08-01 06:18:50
问题 I understand that const_cast works with pointers and references. I'm assuming that the input to const_cast should be a pointer or reference. I want to know why it doesn't remove the constness if the input is a pointer/reference to a const int ? The following code works as expected. const_cast with multilevel pointers int main() { using std::cout; #define endl '\n' const int * ip = new int(123); const int * ptr = ip; *const_cast<int*>(ptr) = 321; cout << "*ip: " << *ip << endl; // value of *ip

Why does const_cast remove constness for a pointer but not for a pointer to a const?

天大地大妈咪最大 提交于 2020-08-01 06:18:28
问题 I understand that const_cast works with pointers and references. I'm assuming that the input to const_cast should be a pointer or reference. I want to know why it doesn't remove the constness if the input is a pointer/reference to a const int ? The following code works as expected. const_cast with multilevel pointers int main() { using std::cout; #define endl '\n' const int * ip = new int(123); const int * ptr = ip; *const_cast<int*>(ptr) = 321; cout << "*ip: " << *ip << endl; // value of *ip

Is it UB to call a non-const method on const instance when the method does not modify members? [duplicate]

拜拜、爱过 提交于 2020-07-05 07:58:05
问题 This question already has answers here : Is const-casting away const-ness of references to actual const objects permitted if they are never modified through them? (2 answers) Const casting empty base class (1 answer) Closed 10 days ago . Code speaks more than thousand words, so... This is undefined behaviour for mutating a const int : struct foo { int x; void modify() { x = 3; } }; void test_foo(const foo& f) { const_cast<foo&>(f).modify(); } int main(){ const foo f{2}; test_foo(f); } What

Is using const_cast for read-only access to a const object allowed?

不问归期 提交于 2020-01-19 16:22:19
问题 In C++ I have a function that only requires read-only access to an array but is mistakenly declared as receiving a non-const pointer: size_t countZeroes( int* array, size_t count ) { size_t result = 0; for( size_t i = 0; i < count; i++ ) { if( array[i] == 0 ) { ++result; } } return result; } and I need to call it for a const array: static const int Array[] = { 10, 20, 0, 2}; countZeroes( const_cast<int*>( Array ), sizeof( Array ) / sizeof( Array[0] ) ); will this be undefined behaviour? If so

const cast to a global var and program crashed (C++)

最后都变了- 提交于 2020-01-10 17:06:02
问题 int main() { const int maxint=100;//The program will crash if this line is put outside the main int &msg=const_cast<int&>(maxint); msg=200; cout<<"max:"<<msg<<endl; return 0; } The function will run ok if the 'const int maxint=100;' definition is put inside the main function but crash and popup a error message said "Access Violation" if put outside. Someone says it's some kind of 'undefined behavior', and i want to know the exact answer and how i can use the const cast safely? 回答1: They are

const cast to a global var and program crashed (C++)

北城以北 提交于 2020-01-10 17:05:54
问题 int main() { const int maxint=100;//The program will crash if this line is put outside the main int &msg=const_cast<int&>(maxint); msg=200; cout<<"max:"<<msg<<endl; return 0; } The function will run ok if the 'const int maxint=100;' definition is put inside the main function but crash and popup a error message said "Access Violation" if put outside. Someone says it's some kind of 'undefined behavior', and i want to know the exact answer and how i can use the const cast safely? 回答1: They are

const cast to a global var and program crashed (C++)

心不动则不痛 提交于 2020-01-10 17:04:04
问题 int main() { const int maxint=100;//The program will crash if this line is put outside the main int &msg=const_cast<int&>(maxint); msg=200; cout<<"max:"<<msg<<endl; return 0; } The function will run ok if the 'const int maxint=100;' definition is put inside the main function but crash and popup a error message said "Access Violation" if put outside. Someone says it's some kind of 'undefined behavior', and i want to know the exact answer and how i can use the const cast safely? 回答1: They are

const_cast doesn't work c++? [duplicate]

谁说我不能喝 提交于 2020-01-09 11:16:34
问题 This question already has answers here : Two different values at the same memory address (7 answers) Closed 2 years ago . I have the following code : const int k=1; int *p=const_cast<int *>( &k); cout<<"k before="<<*p<<endl; *p=10; *const_cast<int *>( &k)=12; cout<<"k after="<<k<<endl; the output was : k before=1 k after=1 why doesn't const cast work here ? 回答1: const_cast is normally used when/if you receive a const pointer to an object that wasn't originally defined as const . If (as in