const-cast

Is it possible to cast a pair<Key, Value> to a pair<const Key, Value>?

百般思念 提交于 2019-11-30 21:40:05
So I have a smart iterator that emulates a map const_iterator, and it needs to build the return type internally. Obviously, I'd like to store a pair<Key, Value> in my iterator class (since I need to modify it), but at the same time I'd like the dereference functions to present a pair<const Key, Value> (actually it would be a const pair<const Key, Value>& and const pair<const Key, Value>* respectively). The only solution I've come up with so far is to dynamically allocate a new pair every time change the value that my iterator class points to changes. Needless to say, this is not a good

Is it possible to cast a pair<Key, Value> to a pair<const Key, Value>?

情到浓时终转凉″ 提交于 2019-11-30 05:28:40
问题 So I have a smart iterator that emulates a map const_iterator, and it needs to build the return type internally. Obviously, I'd like to store a pair<Key, Value> in my iterator class (since I need to modify it), but at the same time I'd like the dereference functions to present a pair<const Key, Value> (actually it would be a const pair<const Key, Value>& and const pair<const Key, Value>* respectively). The only solution I've come up with so far is to dynamically allocate a new pair every time

const_casting question [duplicate]

瘦欲@ 提交于 2019-11-28 13:07:05
This question already has an answer here: Two different values at the same memory address 6 answers I have the following code: int main(){ const int a = 1; const int* b(&a); int* c = const_cast<int*>(b); *c = 29; cout<<*c<<a<<*b; return EXIT_SUCCESS; } Why doesnt the value of 'a' change to 29? Does this mean that the constness of a is not removed when const_casting b? Tommy Andersen Constant variables also allows the compiler certain optimizations, one of these is that the compiler can keep the value in the registers and not reload it. This improves performance but will not work with variables

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

夙愿已清 提交于 2019-11-28 12:26:19
This question already has an answer here: Two different values at the same memory address 6 answers 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 ? 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 your case) the object was originally defined as const , attempting to modify it causes undefined behavior. Without the const_cast ,

How to call a non-const function within a const function (C++)

Deadly 提交于 2019-11-28 10:46:28
I have a legacy function that looks like this: int Random() const { return var_ ? 4 : 0; } and I need to call a function within that legacy code so that it now looks like this: int Random() const { return var_ ? newCall(4) : 0; } The problem is that I'm getting this error: In member function 'virtual int Random() const': class.cc:145: error: passing 'const int' as 'this' argument of 'int newCall(int)' discards qualifiers Now I know in order to fix this error I can make my newCall() a const function. But then I have several funciton calls in newCall() that I have to make, so now I would have to

Is it allowed to cast away const on a const-defined object as long as it is not actually modified?

只愿长相守 提交于 2019-11-28 07:29:42
问题 Is the following allowed: const int const_array[] = { 42 }; int maybe_inc(bool write, int* array) { if (write) array[0]++; return array[0]; } int main() { return maybe_inc(false, const_cast<int *>(const_array)); } In particular, is it OK to cast-away the constness of const_array , which was defined as const, as long as the object is not actually modified, as in the example? 回答1: Yes. This is entirely legal. (It is dangerous, but it is legal.) If you (attempt to) modify a an object declared

Undefined behaviour with const_cast

跟風遠走 提交于 2019-11-28 03:15:42
问题 I was hoping that someone could clarify exactly what is meant by undefined behaviour in C++. Given the following class definition: class Foo { public: explicit Foo(int Value): m_Int(Value) { } void SetValue(int Value) { m_Int = Value; } private: Foo(const Foo& rhs); const Foo& operator=(const Foo& rhs); private: int m_Int; }; If I've understood correctly the two const_casts to both a reference and a pointer in the following code will remove the const-ness of the original object of type Foo,

C++: Why is const_cast evil?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 21:26:18
I keep hearing this statement, while I can't really find the reason why const_cast is evil. In the following example: template <typename T> void OscillatorToFieldTransformer<T>::setOscillator(const SysOscillatorBase<T> &src) { oscillatorSrc = const_cast<SysOscillatorBase<T>*>(&src); } I'm using a reference, and by using const, I'm protecting my reference from being changed. On the other hand, if I don't use const_cast, the code won't compile. Why would const_cast be bad here? The same applies to the following example: template <typename T> void SysSystemBase<T>::addOscillator(const

Correct usage(s) of const_cast<>

白昼怎懂夜的黑 提交于 2019-11-27 18:30:05
As a common rule, it is very often considered a bad practice to use const_cast<>() in C++ code as it reveals (most of the time) a flaw in the design. While I totally agree with this, I however wonder what are the cases were using const_cast<>() is ok and the only solution . Could you guys please give me some examples you know/you encountered ? Thank you very much. const_cast is also used to remove volatile modifiers, as put into practice in this (controversed) article: http://www.drdobbs.com/184403766 it is pretty much designed to be only used with legacy APIs that are not const correct i.e.

behavior of const_cast in C++ [duplicate]

徘徊边缘 提交于 2019-11-27 16:12:48
This question already has an answer here: Two different values at the same memory address 6 answers 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! As-written the way you're doing this is undefined behavior. If you wanted to see the effects of const_cast<> in a defined manner: int a = 5; // note: not const. regular object. const int& cref = a; // const-reference to same