C++

C++ const_cast

For basically every intent, such as processes with data types, interactions with pointers, and processes within classes, C++ is an efficient and potent language of programming that can be used to manipulate information and storage. In C++, const_cast processes could be utilized with structure data types and classes.

The const_cast operator in C++ will be used in this article. Let’s review the casting in C++ before proceeding on.

A methodology for converting one data type or the other is casting. The cast operator is the operator employed for this function. This is an operator that compels conversion among two various data types. A cast operator is an operator in C++ Programming language that requires transforming one data type into another.

The const_cast is one of the cast operators, which transforms between two data types with specific values. An uninitialized value can be changed into an initialized value using the const_cast to associate type method. Const_cast is problematic because it renders the C++ programming language incapable of stopping users from seeking to change a constant object. As a result, this action is undefined.

Example No. 1

A constant or dynamic modifier can be added to or eliminated from a type using the const_cast operator. The const_cast operator will be used in the following examples:

#include <iostream>
using namespace std;
void f(int* q) {
  cout << *q << endl;
}

int main(void) {
  const int i = 30;
  const int* j = &i;
  int* e = const_cast<int*>(j);
  f(e);
  int i1 = 60;
  const int* j1 = &i1;
  int* e1 = const_cast<int*>(j1);
  *e1 = 90;
  return 0;
}

 

Here, we are going to incorporate the library <iostream>. The following standard namespace has been used. We call the function f(). We pass the pointer (* q) as the parameter of this function. The “cout” statement will be used to show the outcome. Here, we have been using the main() method. We will create a variable “i” and set its value as 30. The integer will be specified as its data type, and this variable will be constant. Now, we have been creating a new pointer (*j), and here, we provide the value of the constant variable “i” to this pointer.

The integer data type’s pointer (*e) will be constructed, and we give its value as the const_cast. Then, we change the value of variable “i”. Here, it will not act as a constant variable. By using the value of the variable “i”, we will give the value to the constant pointer (* j1). Then we applied const_cast. We set the value of the pointer “e1”. The return statement is being used.

Example No. 2

In this instance, the variable has a constant value, and the constant pointer is pointing at that variable. However, we may modify the value of the variable by constructing a new pointer having a similar data type and by using const_cast.

#include <iostream>
using namespace std;
int main() {
   const int t = 40;
   const int* u = &t;
   cout<<"The original value:"<<*u<<"\n";
   int* x=const_cast<int *>(u);
   *x=70;
   cout<<"The updated value:"<<*u;
   return 0;
}

 

First of all, the package <iostream> will be imported. The standard namespace will be used in the next line. Let’s start the code in the body of the main() function. A variable named “t” is initialized; its data type is an integer. We give it the value “40.” The keyword “const” is before its data type “int”, so this value would be constant. The value of the constant variable “t” will be specified to the pointer (u) when we construct this pointer.

Then, we utilize the “cout” statement to display the phrase “The original value” on the screen. To utilize const_cast, a new pointer will be constructed. In this case, pointer “x” was generated with a similar integer data type. Thus, we can modify the value of the constant pointer “u” if we give this pointer “u”. This refers to the required constant variable “t”, within const_cast, and allocate a random number to the pointer x. By utilizing const_cast in this method, we have modified the constant value of the variable from 40 to 70. The “cout” statement will then be used to print the modified value on the screen. To terminate the code, the “return” command would have to be entered.

Example No. 3

Modifying a value that is explicitly specified as constant is undefined functionality. Evaluate the following code:

#include <iostream>
using namespace std;
int main() {
   const int t = 40;
   const int* u = &t;
   cout<<"The original value:"<<*u<<"\n";
   int* x=const_cast<int *>(u);
   *x=70;
   cout<<"The updated value:"<<*u;
   return 0;
}

 

At the start of the code, we will include the header file <iostream>. This library will be responsible for input and output methodologies. Then we utilized the standard namespace. Now, we will declare a function. Within this function, we will create a pointer (p). Then we assign the value to the pointer (p). We have employed the return statement, which returns the pointer’s value. We initialize a variable “v”, and the data type of this variable is an integer. We assign it the value “20”. This value will be constant as we have utilized the keyword “const” before initializing this variable.

Here, we construct a new pointer (p_1). And along with this, we have been using const_cast. The function “f(p 1)” aims to modify the value of the constant variable “v” using const cast. We define a function and pass the value of the new pointer as its argument. Next, the “cout” command is used to display the output. In the end, a return statement will be used.

Conclusion

In this article, we have discussed const_cast in C++. Data can be converted from one kind to the other by a procedure called casting. We have executed a few examples that demonstrate the main usage of const_cast. The const_cast word will transform a pointer to a constant entity into a reference to a non-constant variable. The const_cast operator will not be applied to change a variable state directly. An attribute that wasn’t defined as “const” could be effectively modified if its constantness is abandoned.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.