Article ID: 122540 - Last Review: July 5, 2005 - Revision: 2.1 FIX: 'char *' Passed to 'const char * &' Violates Type SafetyThis article was previously published under Q122540 On This PageSYMPTOMS
Visual C++ incorrectly allows a pointer to be passed to a function that
takes a non-const reference to a pointer to a const. This breaks C++ type
safety and allows the const data assigned to the reference to be modified
by the pointer after the function has returned.
RESOLUTION
To protect the const data, the reference should be a reference to a const
pointer to a const, that is: const char * const &.
STATUSThis bug was corrected in Microsoft Visual C++, version 6.0. MORE INFORMATION
A reference may only be initialized with an lvalue expression of the same
type or a class derived from that type. If this is not the case, the
reference must be declared const.
The following sample includes a non-const reference of type const char*, which is being initialized with an expression of type char*. The compiler should, but does not, generate an error. The program prints this string: Sample CodeAPPLIES TO
| Article Translations
|

Back to the top
