Question

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 ?

Was it helpful?

Solution 3

Here is the first statement explained:

[cast to non-const int pointer] ( [get a pointer to 'i' (const)] );

   const_cast<int*>   (        &i            );

Here is the second statement explained:

[cast to non-const int pointer] ( [get value of 'i'] );

   const_cast<int*>    (     i       );

The error is because an integer value is not a pointer value, and so, the const_cast cannot do that cast. It can only map pointers to pointers, or references to references.

Here is the third statement explained:

[cast to non-const int reference] ( [implicitly get a reference to 'i' (const)] );

    const_cast< int& >    (   i  );

Here is the second statement explained:

[cast to non-const int value] ( [get value of 'i' (const)] );

   const_cast< int >      (     i   );

The error is because the const_cast cannot be used to cast between values, only between pointers or references. For values, we talk about "conversions" not casts. As in:

int i_nc = i; // OK: no need for const-cast since the value is copied. A conversion is a method to copy the value of an object of one type into an object of another type. Casting operators don't make sense for that purpose.

OTHER TIPS

1) You are casting (const int*) to (int*). So because of const modifier you can't change the value that is placed at that address(pointer point to some address in memory). When you cast it to (int*) compiler will allow change data at that address.

2) You are trying to cast (const int) to pointer to int (int*). (int) and (int*) are different types. This is the same as ask const_cast to cast string to float. The const_cast operator can't change the type of the variable. To make such things you should look to static_cast or reinterpret_cast.

3) You cast const int to reference to int and assign the value to int(you simply copied the value to a new variable). This is probably not exactly you wanted, because changing j in this case doesn't change i. You can create a reference to int instead of j and then you can change the value of i.

4) I don't understand what you are trying to do here. The idea of the const_cast is to remove the const protection on object. So this operation is possible only on pointers and references. You don't need any cast to copy const int to int. But you can't change the value of i until you take pointer or reference and remove the protection.

Conclusion.Removing a const is a bad style of programming. Assume you wrote a library where a function has const int* argument. The user of your library will be sure that his int won't change but you changed it and he lost the data he needed.

When you write

int *j = const_cast<int*>(i); 

you are trying to convert 'i' to a pointer. const_cast is not meant to be used for changing the data type.

maybe you meant

int *j = const_cast<int*>(&i); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top