const_cast in C++ - Type casting operators

C++Server Side ProgrammingProgramming

Given the task is to show the working of const_cast in c++.

const_cast is one of the type casting operators. It is used to change the constant value of any object or we can say it is used to remove the constant nature of any object.

const_cast can be used in programs that have any object with some constant value which need to be changed occasionally at some point.

Syntax

The syntax is as follows −

const_cast<type name>(expression)

Example

Input:
const int x = 50;
const int* y = &x;
cout<<"old value is"<<*y<<"\n";
int* z=const_cast<int *>(y);
*z=100;
cout<<"new value is"<<*y;
Output: old value is 50
new value is 100

Following example shows the basic use of const_cast. Here we have declared a constant variable ”x” of type int which has been assigned a value of 50 and another constant pointer “y” of type int which points at the variable “x”.

A third pointer has to be created to use const_cast, and here we have created pointer ”z” of the same data type, that is, int.

So when we pass our constant pointer “y”,that points at constant variable “x” into const_cast, and assign a value to the pointer z, we are able to make changes to the value of our constant pointer “y”.

This way we were able to change the constant value from 50 to 100 using const_cast.

If we try to change the value of “x” that the pointer “y” is pointing at without using const_cast , then the following error will be shown-“assignment of read-only location”

Example

Input:
const int x = 50;
const int* y = &x;
cout<<"old value is"<<*y<<"\n";
int* z=const_cast<int *>(y);
*z=100;
cout<<"new value is"<<*y;
Output: old value is

Approach used in the below program as follows

  • First create a constant variable of type int and give it some suitable size, let’s say “a” and its value be 20.
  • Then create a constant pointer, let us say “b” of the same data type and allocate it the address of our constant variable “a”.
  • Then create a third pointer, let us say “c” of data type int to be used for const_cast.
  • Now pass our constant pointer “b” into const_cast and keep it equal to our pointer “c”.
  • Finally make changes in the value of our pointer “c”. This will automatically make changes in the value at which our constant pointer “b” is pointing.

Algorithm

Start
Step 1 -> In function main()
Declare a constant int a=20
Declare a constant pointer int* b=&a
Declare a pointer int*c = const_cast<int *>(b)
Assign *c=40
Stop

Example

 Live Demo

#include <iostream>
using namespace std;
int main() {
   const int a = 20;
   const int* b = &a;
   cout<<"old value is"<<*b<<"\n";
   int* c=const_cast<int *>(b);
   *c=40;
   cout<<"new value is"<<*b;
   return 0;
}

Output

If we run the above code it will generate the following output −

old value is 20
new value is 40

Here, the constant pointer “b” is pointed at the constant variable “a” with value=20 which is unchangeable. But by creating a third non-constant pointer “c” of the same data type and using const_cast we are able to change that constant value.

The change of value of pointer “c” resulted in the change of the constant value 20 at which the constant pointer “b” was pointing. Therefore before using const_cast the output value was 20 and after using it the output value was 40.

Other uses of const_cast

In any program, const_cast can be used to pass constant data to another function that does not accept constant data.

Example

#include <iostream>
using namespace std;
int change(int* p2) {
   return (*p2 * 10);
}
int main() {
   const int num = 100;
   const int *p = #
   int *p1 = const_cast <int *>(p);
   cout << change(p1);
   return 0;
}

Output

If we run the above code it will generate the following output −

1000

The following program shows how we are able to pass a constant value of 100 using const_cast into the function change() that does not receive any constant data.

The change() function receives the value and multiplies it by 10 and returns it back to the main() function that generates the final output, that is, 1000.

If we run the same program without const_cast and try to pass the constant value directly into the change() function, it will show errors.

raja
Published on 20-Jan-2020 07:34:35
Advertisements