C++ 11 feature: const_cast

“const_cast” is new concept in C++ 11.

const_cast is used to remove the constantness from references and pointers.

I agree we used const pointer so to avoid un-necessary data modification. But there will be some instances where you need to modify the const data. In such situation “const_cast” will be useful.

Syntax of const_cast cast:

data_type *var_name = const_cast <data_type *>(pointer_variable);

Example 1:

You have an const integer pointer and you need to assign that to a non const integer pointer it will result in an error.

Example:

#include <iostream>
#include <set>
//for more C++ tutorial visit www.ProDeveloperTutorial.com
using namespace std;

int main() 
{ 
    int inum = 10;
    
    const int *iPtr = &inum; // correct
    
    int *iPtr2 = &iPtr; // wrong
    
    return 0; 
}

Error:

Main.cpp:12:10: error: cannot initialize a variable of type 'int *' with an rvalue of type 'const int **'
    int *iPtr2 = &iPtr;
         ^       ~~~~~
1 error generated.

Why? Because you cannot assign “const int ptr” to “non const int ptr”.

Example 2: We can solve this problem by using “const_cast”, Example:

#include <iostream>
#include <set>
//for more C++ tutorial visit www.ProDeveloperTutorial.com
using namespace std;

int main() 
{ 
    int inum = 10;
    
    const int *iPtr = &inum;
    
    int *iPtr2 =  const_cast<int*> (iPtr); // works
    
    return 0; 
}

Note: You cannot use “const_char” to a vairable that is actually constatn. This will result in undefined behaviour.

Example 3: The variable is not declared as constant:

#include <iostream>
#include <set>
//for more C++ tutorial visit www.ProDeveloperTutorial.com
using namespace std;

int main() 
{ 
    int inum = 10;
    
    const int &ref = inum; // created a constant reference variable
 
    const int *iPtr = &inum; // created a constant int pointer variable

    // we shall change the value
    
    const_cast<int &> (ref) = 4;
    cout<<"The value of inum = "<<inum<<endl;
    
    *const_cast<int*> (iPtr) = 14;
    cout<<"The value of inum = "<<inum<<endl;


    return 0; 
}

Output:

The value of inum = 4
The value of inum = 14

Example 4: The variable is declared as constant:

#include <iostream>
#include <set>
//for more C++ tutorial visit www.ProDeveloperTutorial.com
using namespace std;

int main() 
{ 
    const int inum = 10;
    
    const int &ref = inum; // created a constant reference variable
 
    const int *iPtr = &inum; // created a constant int pointer variable

    // we shall change the value
    
    const_cast<int &> (ref) = 4;
    cout<<"The value of inum = "<<inum<<endl;
    
    *const_cast<int*> (iPtr) = 14;
    cout<<"The value of inum = "<<inum<<endl;

    return 0; 
}

This will result in undefined behaviour. The program might run fine or might crash.

Example 5: “const_cast” is used to remove constantness of a pointer.

#include <iostream>
#include <set>
//for more C++ tutorial visit www.ProDeveloperTutorial.com
using namespace std;
  
int fun(int* ptr) 
{ 
    *ptr = *ptr + 30; 
    return (*ptr); 
} 
  
int main(void) 
{ 
    int val = 10; 
    const int *ptr = &val; 
    
    int *ptr1 = const_cast <int *>(ptr); 
    
    fun(ptr1); 
    
    cout << val; 
    return 0; 
} 

Output:

40

 

Example 6: “const_cast” is used to remove volatile of an attribute.

#include <iostream>
#include <set>
//for more C++ tutorial visit www.ProDeveloperTutorial.com
using namespace std;
int main(void) 
{ 
    int a1 = 10; 
    const volatile int* vPtr = &a1; 
    
    cout << "typeid of vPtr " << typeid(vPtr).name() <<endl; 
    
    int* iPtr = const_cast <int *> (vPtr); 
    cout << "typeid of iPtr " << typeid(iPtr).name() << endl; 
    return 0; 
} 

Output:

typeid of vPtr PVKi
typeid of iPtr Pi
Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *