Content area
Full text
The type conversion rules of both C and C++ often require that when const or volatile qualifiers appear in the type of a function argument, the function's corresponding parameter must have a similarly qualified type. C and C++ have slightly different rules regarding the meaning of const and volatile qualifers in parameter declarations, largely because C++ supports function name overloading while C does not.1
A ripple of qualifiers
When I introduced function name overloading, I said that using the const and volatile qualifiers in data declarations causes a ripple that spills over into function declarations. I didn't elaborate then, but I will now with the following example.
Suppose that:
to display the message.
Now, suppose that you add the const qualifier to the data definition, as in:
so the text of the message can reside in read-only memory (ROM) . If you make no changes to the function declaration, then both C and C++ will reject the call on (1) for the following reason.
The call passes the address of the initial character of message as the argument to put. The variable message has type "array of const char," so each element in the array has type "const char," and the argument passed to the call has type "pointer to const char." However, put's parameter has type "pointer to (non-const) char." As I explained in my last column, conversion from "pointer to const char" to "pointer to char" is not a valid conversion.2 Neither C nor C++ permits this conversion, unless you use a cast.
Casts are nasty because they force the...