const char * vs char const * 
Author Message
 const char * vs char const *

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; ????

Dazed and confused.

Chris.
--

< http://www.*-*-*.com/ ;



Mon, 21 Jun 1999 03:00:00 GMT  
 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!"
--------------------------------------------------------------------------------



Mon, 21 Jun 1999 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Help: inline char const* const& max(char const* const &a, char const* const &b)

2. const char *p == char const *p ?

3. const char* vs. char *

4. Why not static const char * const __func__ ?

5. meaning of const char *const ?

6. Conversion const char* -> const unsigned short*

7. char** and const char** ...

8. va_arg(ap, const char *) = char*?

9. const char* to char[64]

10. char^ to const char* casting

11. const char ** incompatible with char ** (and vice versa)?

12. passing const char* to char* fails

 

 
Powered by phpBB® Forum Software