C++ examples for Data Type:Cast
const_cast operator for casting away const or volatile qualification.
#include <iostream> #include <cstring> // contains prototypes for functions strcmp and strlen #include <cctype> // contains prototype for function toupper using namespace std; const char *maximum( const char *first, const char *second ) { return ( strcmp( first, second ) >= 0 ? first : second ); } int main() // w w w . jav a 2 s.c o m { char s1[] = "hello"; // modifiable array of characters char s2[] = "goodbye" ; // modifiable array of characters // const_cast required to allow the const char * returned by maximum // to be assigned to the char * variable maxPtr char *maxPtr = const_cast< char * >( maximum( s1, s2 ) ); cout << "The larger string is: " << maxPtr << endl; for ( size_t i = 0; i < strlen( maxPtr ); ++i ) maxPtr[ i ] = toupper( maxPtr[ i ] ); cout << "The larger string capitalized is: " << maxPtr << endl; }