21.2. const_cast Operator

C++ provides the const_cast operator for casting away const or volatile qualification. You declare a variable with the volatile qualifier when you expect the variable to be modified by hardware or other programs not known to the compiler. Declaring a variable volatile indicates that the compiler should not optimize the use of that variable because doing so could affect the ability of those other programs to access and modify the volatile variable.

In general, it’s dangerous to use the const_cast operator, because it allows a program to modify a variable that was declared const. There are cases in which it’s desirable, or even necessary, to cast away const-ness. For example, older C and C++ libraries might provide functions that have non-const parameters and that do not modify their parameters—if you wish to pass const data to such a function, you’d need to cast away the data’s const-ness; otherwise, the compiler would report error messages.

Similarly, you could pass non-const data to a function that treats the data as if it were constant, then returns that data as a constant. In such cases, you might need to cast away the const-ness of the returned data, as we demonstrate in Fig. 21.1.


 1   // Fig. 21.1: fig21_01.cpp
 2   // Demonstrating const_cast.
 3   #include <iostream>
 4   #include <cstring> // contains prototypes for functions strcmp and strlen
 5   #include <cctype> // contains prototype for function toupper
 6   using namespace std;
 7
 8   // returns the larger of two C strings
 9   const char *maximum( const char *first, const char *second )
10   {
11      return ( strcmp( first, second ) >= 0 ? first : second );
12   } // end function maximum
13
14   int main()
15   {
16      char s1[] = "hello"; // modifiable array of characters
17      char s2[] = "goodbye"; // modifiable array of characters
18
19      // const_cast required to allow the const char * returned by maximum
20      // to be assigned to the char * variable maxPtr
21      char *maxPtr = const_cast< char * >( maximum( s1, s2 ) );
22
23      cout << "The larger string is: " << maxPtr << endl;
24
25      for ( size_t i = 0; i < strlen( maxPtr ); ++i )
26         maxPtr[ i ] = toupper( maxPtr[ i ] );
27
28      cout << "The larger string capitalized is: " << maxPtr << endl;
29   } // end main


The larger string is: hello
The larger string capitalized is: HELLO


Fig. 21.1. Demonstrating operator const_cast.

In this program, function maximum (lines 9–12) receives two C strings as const char * parameters and returns a const char * that points to the larger of the two strings. Function main declares the two C strings as non-const char arrays (lines 16–17); thus, these arrays are modifiable. In main, we wish to output the larger of the two C strings, then modify that C string by converting it to uppercase letters.

Function maximum’s two parameters are of type const char *, so the function’s return type also must be declared as const char *. If the return type is specified as only char *, the compiler issues an error message indicating that the value being returned cannot be converted from const char * to char *—a dangerous conversion, because it attempts to treat data that the function believes to be const as if it were non-const data.

Even though function maximum believes the data to be constant, we know that the original arrays in main do not contain constant data. Therefore, main should be able to modify the contents of those arrays as necessary. Since we know these arrays are modifiable, we use const_cast (line 21) to cast away the const-ness of the pointer returned by maximum, so we can then modify the data in the array representing the larger of the two C strings. We can then use the pointer as the name of a character array in the for statement (lines 25–26) to convert the contents of the larger string to uppercase letters. Without the const_cast in line 21, this program will not compile, because you are not allowed to assign a pointer of type const char * to a pointer of type char *.


Image Error-Prevention Tip 21.1

In general, a const_cast should be used only when it is known in advance that the original data is not constant. Otherwise, unexpected results may occur.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.9.179