Re: Cstring, string ,and char[]
On Mon, 18 Jun 2007 10:33:00 -0400, Joseph M. Newcomer
<newcomer@flounder.com> wrote:
Generally, there is no need to use std::string in MFC code, so avoid its use.
I tend to agree with Joe on this point. IMHO, to manage strings in MFC
it is fine to use CString (in fact CString is very good integrated
with several MFC classes, e.g. in CWnd and CWnd-derived classes; etc.)
However, it is fine to use other STL classes like containers (e.g.
std::vector, std::map) in MFC, too.
There would be specific reasons to convert between CString and
std::string, e.g. if you develop (as I like) a GUI using MFC and the
"kernel" of the app in "pure" C++, using some of STL classes, etc.
So the GUI front-end and the kernel back-end need to communicate each
other, and conversions between CString and std::string (or
std::wstring) could be required.
If you want to convert between CString and std::string, as I wrote in
another post, I have used code like this:
inline std::string CStringToStdString( const CString & s )
{
CT2A dest( s );
return std::string( (const char *) dest );
}
inline CString StdStringToCString( const std::string & s )
{
CA2T dest( s.c_str() );
return CString( (LPCTSTR) dest );
}
e.g.
CString str;
str = _T("Hello");
std::string s = CStringToStdString(str);
The above code is based on "CX2Y" ATL conversion helpers.
MrAsm