-
December 26th, 2012, 09:27 AM
#1
Got confused with const_cast
Sample_Program-1
#include<iostream>
using namespace std ;
int main(){
const int i = 9;
int *j = const_cast<int*>(&i); //Ok
int *j = const_cast<int*>(i); //Error
}
Sample_Program-2
#include<iostream>
using namespace std ;
int main(){
const int i = 9;
int j = const_cast<int&>(i);//Ok
int j = const_cast<int>(i);//Error
}
I was just learning some c++ concept and met with the above 2 concepts . Can anyone please explain the concept i marked as error in the above 2 sample program ?
-
December 26th, 2012, 09:50 AM
#2
Re: Got confused with const_cast
Your compiler should have given you some error messages. What do they say and what do you think they mean?
-
December 26th, 2012, 10:12 AM
#3
Re: Got confused with const_cast
My compiler gives only error message but dont explain it . I am new to c++ . Thats why i have mentioned in my post "Can anyone please explain the conpt i marked as error in the above 2 sample program ?"
-
December 26th, 2012, 10:22 AM
#4
Re: Got confused with const_cast
Originally Posted by vikuseth
My compiler gives only error message but dont explain it .
Well, for your first example (once I fix an irrelevant error whereby you declare j twice), my compiler complains that there was an:
Code:
invalid const_cast from type 'const int' to type 'int*'
Doesn't that explain to you what is the problem? The problem is that the const_cast is invalid. Why is it invalid? Because a const_cast is supposed to be used to remove (or add) const-ness, but here you are also changing from a non-pointer type to a pointer type.
For your next example, I get:
Code:
invalid use of const_cast with type 'int', which is not a pointer, reference, nor a pointer-to-data-member type
This tells you that the const_cast is invalid because what you are trying to cast "is not a pointer, reference, nor a pointer-to-data-member type".
So, did you actually read the error messages that your compiler gave you and tried to make sense to what they said?
-
December 26th, 2012, 10:30 AM
#5
Re: Got confused with const_cast
So constant cast only works for pointer , reference or pointer to datamember ?
-
December 26th, 2012, 10:41 AM
#6
Re: Got confused with const_cast
Originally Posted by vikuseth
So constant cast only works for pointer , reference or pointer to datamember ?
Consider your second example. You could have done without a cast:
The fact that i is const does not matter since its value is copied to j.
On the other hand, consider:
Code:
const int x = 123;
const int* i = &x;
int* j = i;
Now this is a problem, because you could potentially write:
which would result in undefined behaviour since you're changing a constant. However, maybe for some reason (e.g., working with a legacy API) you really do need j to be an int*, not a const int*, but you know for sure that the value will not be changed through the pointer. In that case, it is appropriate to write:
Code:
int* j = const_cast<int*>(i);
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|