
const char * vs char const *
Quote:
>I am told its good c++ practice to use the const keyword on things that
>shouldn't be changed :) My big problwm with this comes in when ppl
>"protect" strings by declaring them as follows:
>void somefunc(const char* psztext);
>or even:
>void somefunc(LPCTSTR psztext);
>Now, as great as this looks, the const here is only preventing the pointer
>from being changed;
>*pszText = '0'; // will compile without a hitch.
>while:
>while(*psztext)
> psztext++; // won't compile even though the string is left untouched.
>declaring strings as "char const*" seems to have the desired effect though.
>The pointer can be changed (to scan thru the string), but any attempt to
>write will flag a compiler error.
>Am I the only one confused by this? Why is LPCTSTR: typedef CONST CHAR
>*LPCSTR, *PCSTR;
>rahter than the more (sensible?) typedef CHAR CONST *LPCSTR, *PCSTR; ????
hmmmm....I get a compile error:
C:\Development\Projects\RemoteHubManager\XIODeviceInfo.h(72) : error C2166:
l-value specifies const object
please review your C++ reference manual: r.8.2.1, Pointers, Pg. 528
of Stroustrup's book.
const char *const xyz
^^^^^ ^^^^^
1 2
the first const is of the data POINTED TO by xyz;
the scond const is that of the POINTER itself!
--------------------------------------------------------------------------------
XIOtech Corporation Personal Home Page: http://www.visi.com/~nde
612-828-5961
"Question authority...and the authorities will end up questioning YOU!"
--------------------------------------------------------------------------------