Re: CString to const char*
- From: Ulrich Eckhardt <eckhardt@xxxxxxxxxxxxxx>
- Date: Wed, 27 Sep 2006 16:48:49 +0200
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
.
- Follow-Ups:
- Re: CString to const char*
- From: David Wilkinson
- Re: CString to const char*
- From: NickP
- Re: CString to const char*
- References:
- CString to const char*
- From: NickP
- Re: CString to const char*
- From: Ulrich Eckhardt
- Re: CString to const char*
- From: NickP
- CString to const char*
- Prev by Date: Re: VC++2005 executables much slower than VC++6????
- Next by Date: Re: saving a word document in a runtime path and in a special path
- Previous by thread: Re: CString to const char*
- Next by thread: Re: CString to const char*
- Index(es):
Relevant Pages
|