C++ supports following 4 types of casting operators:
const_cast is used to cast away the constness of variables. Following are some interesting facts about const_cast.
1) const_cast can be used to change non-const class members inside a const member function. Consider the following code snippet. Inside const member function fun(), ‘this’ is treated by the compiler as ‘const student* const this’, i.e. ‘this’ is a constant pointer to a constant object, thus compiler doesn’t allow to change the data members through ‘this’ pointer. const_cast changes the type of ‘this’ pointer to ‘student* const this’.
Old roll number: 3 New roll number: 5
2) const_cast can be used to pass const data to a function that doesn’t receive const. For example, in the following program fun() receives a normal pointer, but a pointer to a const can be passed with the help of const_cast.
3) It is undefined behavior to modify a value which is initially declared as const. Consider the following program. The output of the program is undefined. The variable ‘val’ is a const variable and the call ‘fun(ptr1)’ tries to modify ‘val’ using const_cast.
It it fine to modify a value which is not initially declared as const. For example, in the above program, if we remove const from declaration of val, the program will produce 20 as output.
4) const_cast is considered safer than simple type casting. It’safer in the sense that the casting won’t happen if the type of cast is not same as original object. For example, the following program fails in compilation because ‘int *’ is being typecasted to ‘char *’
prog.cpp: In function ‘int main()’: prog.cpp:8: error: invalid const_cast from type 'const int*' to type 'char*'
5) const_cast can also be used to cast away volatile attribute. For example, in the following program, the typeid of b1 is PVKi (pointer to a volatile and constant integer) and typeid of c1 is Pi (Pointer to integer)
typeid of b1 PVKi typeid of c1 Pi
Predict the output of following programs. If there are compilation errors, then fix them.
—Aashish Barnwal. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- reinterpret_cast in C++ | Type Casting operators
- static_cast in C++ | Type Casting operators
- What are the operators that can be and cannot be overloaded in C++?
- Increment (Decrement) operators require L-value Expression
- Order of operands for logical operators
- Advanced C++ | Conversion Operators
- Bitwise Operators in C/C++
- How to sum two integers without using arithmetic operators in C/C++?
- Execution of printf with ++ operators
- Conditionally assign a value without using conditional and arithmetic operators
- new and delete operators in C++ for dynamic memory
- Overloading stream insertion (<>) operators in C++
- Operators in C / C++
- Unary operators in C/C++
- Written version of Logical operators in C++
- Comparing String objects using Relational Operators in C++
- Left Shift and Right Shift Operators in C/C++
- Relational Operators on STL Array in C++
- unordered_set operators in C++ STL
- Difference between Increment and Decrement Operators