const_cast<type> (expr): The const_cast operator is used to explicitly override const and/or volatile in a cast. The target type must be the same as the source type except for the alteration of its const or volatile attributes. This type of casting manipulates the const attribute of the passed object, either to be set or removed.
This type of casting manipulates the constness of the object pointed by a pointer, either to be set or to be removed.
See the below code snippet
1 2 3 4 5 6 7 8 9 10 11 12 |
#include "stdafx.h" #include <iostream> #include <typeinfo> using namespace std; void GetVal (char * pcchStr){ cout << pcchStr << '\n'; } int main () { const char * pcchVal = "sample"; GetVal ( const_cast<char *> (pcchVal) ); getchar (); return 0; } |
Recommended Tutorials: