
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Const cast in C++
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: 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β
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
#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.