Re: CString to const char*

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



NickP wrote:
>>Also, never cast unless you exactly know what you are doing.

That's the whole point of trial and error, I've been programming for
quite a few years now, that comment wasn't particularly necessary to be
honest.

Sorry, I didn't intend to sound arrogant or patronizing. The thing I wanted
to express is that only very, very few cases require a cast and only very
very few of those require a C style cast. Most conversions in fact happen
automatically, and if not, the compiler usually has a good reason. Adding
casts might silence the compiler easily, but, as you saw yourself when
casting wchar_t* to char*, the result is often just wrong.

>>Define 'better' before making such statements.

Well if you are going to be pedantic about it, don't worry.

No, what I meant was that depending on the context, either might be the
better choice. I use both CString and std::string/std::wstring here,
depending on whether I'm working in an MFC environment or in portable C++
code.

Since you mentioned gethostbyname, I'd go and take CStringA. In particular
the GetBufferSetLength(), giving you a mutable buffer, makes it a slightly
better choice for interfacing with such C style APIs.

Otherwise, CString (the TCHAR based version) also has a constructor that
takes a char*, so you can use plain char strings with gethostbyname and
have CString automatically convert them to TCHAR. However, the other way
round is not as easy, which I first thought was your goal.

Uli

.



Relevant Pages

  • Re: CString to const char*
    ... very few of those require a C style cast. ... Most conversions in fact happen ... but I thought CString was supposed to convert to ... I know m CString object is the TCHAR based version as I recently ...
    (microsoft.public.vc.language)
  • Re: Application crashes on CString.Format() call
    ... (LPCTSTR), not char* which requires GetBuffer or a dangerous const_cast. ... >>>I always cast all CStrings to LPCTSTR just to be sure. ... If you pass a CString to a function expecting a char *, ...
    (microsoft.public.vc.language)
  • Re: CString to const char*
    ... hostent* pHETIP = NULL; ... casting wchar_t* to char*, the result is often just wrong. ... but I thought CString was supposed to convert ... I know m CString object is the TCHAR based version as I recently ...
    (microsoft.public.vc.language)
  • Re: CString to const char*
    ... means make them based on TCHAR, ... I need to use TCHAR type, it's being used throughout the application, the only thing is, now I am using the sockets functions and they are asking for const char* input. ... I've seen code examples passing just CString so it got me a little confused. ... But the receiving end then gets also unicode string. ...
    (microsoft.public.vc.language)
  • Re: Conversion of CString to char[]
    ... > CString str; ... > CString and cast it to char* (this is guaranteed to work by the ... you don't take the address - you cast the string itself to char* (or ...
    (microsoft.public.vc.language)