[RESOLVED] How does const_cast<> work?
Hello,
const_cast<> supposed to "cast away const" but it does not. The output is: 1002 1001, I expect it to be 1002 1002. Could someone explain what's going on? Thx.
Code:
#include<iostream>
using namespace std;
int main(int argc, char* argv[])
{
const int ci = 1001;
int* i1 = const_cast<int*>(&ci);
*i1 = 1002;
cout << *i1 << " " << ci << endl;
return 0;
}
Re: How does const_cast<> work?
Quote:
Originally Posted by
vincegata
const_cast<> supposed to "cast away const" but it does not. The output is: 1002 1001, I expect it to be 1002 1002. Could someone explain what's going on? Thx.
Code:
#include<iostream>
using namespace std;
int main(int argc, char* argv[])
{
const int ci = 1001;
int* i1 = const_cast<int*>(&ci);
*i1 = 1002;
cout << *i1 << " " << ci << endl;
return 0;
}
Your program is ill-formed. According to the standard, you are not allowed to assign to the result of a const_cast when the original is defined as const. "Casting away const" just means that you are circumventing the type system. You are not changing any actual characteristics of the data in your program. The only real use for it is when you need to use a C interface that does not support const. But you should only use it when you are sure that the result of the const_cast will not be written to.
Re: How does const_cast<> work?
const_cast isn't diffent from the C-style of casting away a const.
it's the programmer's responsability to make sure that removing the constness is actually safe.
Countrary to D_Drmmr's statment, you CAN write to a object that has it's const removed. But only if you can guarantee safety thereof yourself.
you cannot safely use it to write to a variable that was defined as const because said variable may be in a readonly memorypage (or ROM).
writing could also have an impact on optimisations, the code "outside" of the const cast bit may very well assume that no changes can happen and the compiler may use that to optimize code according to that. writing to it anyway could have all kinds of undesired effects.
const_cast overrides the compiler, you're responsible for what happens.
Re: How does const_cast<> work?
Quote:
Originally Posted by
vincegata
Hello,
const_cast<> supposed to "cast away const" but it does not. The output is: 1002 1001, I expect it to be 1002 1002. Could someone explain what's going on? Thx.
To add, try changing that to a string-literal instead of an int, cast the constness away, and then change it. You will see that your program will more than likely crash.
Code:
#include<iostream>
using namespace std;
int main(int argc, char* argv[])
{
const char * ci = "1001";
char* i1 = const_cast<char*>(ci);
*i1 = '8';
return 0;
}
What is expected to happen is that the first character should be an '8' instead of '1', right? You'll be lucky if you even get to the return statement at the end.
Regards,
Paul McKenzie
Re: How does const_cast<> work?
Quote:
Originally Posted by OReubens
Countrary to D_Drmmr's statment, you CAN write to a object that has it's const removed.
The way I interpreted it, D_Drmmr did not say that you cannot "write to a object that has it's const removed". Rather, the statement was that "you should only use it when you are sure that the result of the const_cast will not be written to" (emphasis mine). This makes sense to me because if the const-ness was introduced somewhere, then there would have been an implied promise that the object would not be written to in that context. As such, I agree with D_Drmmr that the common use is when the object is passed to a C (or sometimes even C++) API that is not const-correct, but which say, effectively guarantees the const-ness in the documentation.
Re: How does const_cast<> work?