const-cast

const_cast: same address but different value? [duplicate]

女生的网名这么多〃 提交于 2019-12-31 04:47:03
问题 This question already has answers here : Two different values at the same memory address (7 answers) Closed 2 years ago . New to C++ and learning the const_cast — get really confused by the code below: int main(){ const int j = 1; int * p = (int *)(&j); cout << j << ' ' << *p << endl; cout << &j << ' ' << p << endl; *p = 2; cout << j << ' ' << *p << endl; cout << &j << ' ' << p << endl; const int k = 1; int * q = const_cast<int*>(&k); cout << k << ' ' << *q << endl; cout << &k << ' ' << q <<

behavior of const_cast in C++ [duplicate]

会有一股神秘感。 提交于 2019-12-28 04:31:08
问题 This question already has answers here : Two different values at the same memory address (7 answers) Closed 2 years ago . Here is my problem, the problem is in comments const int a = 5; const_cast<int&>(a)=7; //throw over const attribute in a,and assign to 7 std::cout<<a<<std::endl; //why still out put 5!!!!!!!!!! Who can tell me why, and some books account these problems to recommend ? Thanks! 回答1: As-written the way you're doing this is undefined behavior. If you wanted to see the effects

2 value in one variable (const and const_cast) c++ [closed]

牧云@^-^@ 提交于 2019-12-25 06:43:42
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 years ago . void main() { const int a = 10; const int *b = &a; int *c = const_cast <int*>(b); *c = 5; cout<<a<<" "<<*b<<" "<<*c<<endl; //10 5 5 cout<<&a<<" "<<b<<" "<<c<<endl; //same address cout<<*(int*)&a<<" "<<*&a<<endl; //5 10 } what makes type cast affected this? where is the value stored? 回答1: The

Explanation of the UB while changing data

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-25 04:08:27
问题 I was trying to demonstrate to a work pal that you can change the value of a constant-qualified variable if really wants to (and knows how to) by using some trickery, during my demostration, I've discovered that exists two "flavours" of constant values: the ones that you cannot change whatever you do, and the ones that you can change by using dirty tricks. A constant value is unchangeable when the compiler uses the literal value instead of the value stored on the stack (readed here), here is

C++ const cast, unsure if this is secure

让人想犯罪 __ 提交于 2019-12-23 08:48:26
问题 It maybe seems to be a silly question but i really need to clarify this: Will this bring any danger to my program? Is the const_cast even needed? If i change the input pointers values in place will it work safely with std::string or will it create undefined behaviour? So far the only concern is that this could affect the string "some_text" whenever I modify the input pointer and makes it unusable. std::string some_text = "Text with some input"; char * input = const_cast<char*>(some_text.c_str

const_cast in template. Is there a unconst modifier?

不羁岁月 提交于 2019-12-22 06:59:25
问题 I have a template class like this: template<T> class MyClass { T* data; } Sometimes, I want to use the class with a constant type T as follows: MyClass<const MyObject> mci; but I want to modify the data using const_cast<MyObject*>data (it is not important why but MyClass is a reference count smart pointer class which keeps the reference count in the data itself. MyObject is derived from some type which contains the count. The data should not be modified but the count must be modified by the

Need clarifications in C-style, reinterpret, and const casts

微笑、不失礼 提交于 2019-12-22 05:29:30
问题 Am I right in assuming that C-style casts (which are discouraged) are nothing but reinterpret_casts? Using the latter is visually striking and easy to search when looking for nasty casts, and hence it's recommended over C-style casts? If casting away const using const_cast and writing to a originally const object is undefined, what is the purpose of const_cast? Note: I know that Bjarne rightly condemns casting operations that they are unsafe and even goes to the extent of stating "An ugly

Need clarifications in C-style, reinterpret, and const casts

落花浮王杯 提交于 2019-12-22 05:28:59
问题 Am I right in assuming that C-style casts (which are discouraged) are nothing but reinterpret_casts? Using the latter is visually striking and easy to search when looking for nasty casts, and hence it's recommended over C-style casts? If casting away const using const_cast and writing to a originally const object is undefined, what is the purpose of const_cast? Note: I know that Bjarne rightly condemns casting operations that they are unsafe and even goes to the extent of stating "An ugly

Does casting away constness from “this” and then changing a member value invoke undefined behaviour?

依然范特西╮ 提交于 2019-12-22 05:10:39
问题 In a response to my comment to some answer in another question somebody suggests that something like void C::f() const { const_cast<C *>( this )->m_x = 1; } invokes undefined behaviour since a const object is modified. Is this true? If it isn't, please quote the C++ standard (please mention which standard you quote from) which permits this. For what it's worth, I've always used this approach to avoid making a member variable mutable if just one or two methods need to write to it (since using

C++ const_cast usage instead of C-style casts

浪子不回头ぞ 提交于 2019-12-19 17:44:32
问题 Why is the following?: const int i0 = 5; //int i1 = const_cast<int>(i0); // compilation error int i2 = (int)i0; // okay int i3 = 5; //const int i4 = const_cast<const int>(i3); // compilation error const int i5 = (const int)i3; // okay 回答1: const int i0 = 5; //int i1 = const_cast<int>(i0); // compilation error int i2 = (int)i0; // okay int i3 = 5; //const int i4 = const_cast<const int>(i3); // compilation error const int i5 = (const int)i3; // okay The compilation errors are caused because you