All Questions

79
votes
7answers
43815 views

Is const_cast safe?

I can't find much information on const_cast. The only info I could find (on Stack Overflow) is: The const_cast<>() is used to add/remove const(ness) (or volatile-ness) of a variable. This...
1
votes
4answers
127 views

Strange behavior of const_cast

Consider the following code: I declare a new reference end assign it to value a via const_cast. Then I just increase the reference value print the addresses and values. #include <iostream> usi...
4
votes
3answers
294 views

behavior of const_cast in C++

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...
12
votes
2answers
1668 views

How to convert "pointer to pointer type" to const?

With the following code void TestF(const double ** testv){;} void callTest(){ double** test; TestF(test); } I get this: 'TestF' : cannot convert parameter 1 from 'double **' to 'const do...
36
votes
7answers
13757 views

Correct usage(s) of const_cast<>

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 ...
11
votes
9answers
7527 views

C++: Why is const_cast evil?

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>::setOscil...
18
votes
7answers
19448 views

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

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() cons...
4
votes
3answers
369 views

const_cast doesn't work c++?

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; cou...
1
votes
1answers
747 views

const_casting question

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...
5
votes
5answers
1143 views

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

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 resul...

Previous Next