Re: string vs string*
"Alf P. Steinbach" <alfps@start.no> ha scritto nel messaggio
news:xqednUmvldsVW5bVnZ2dnUVZ_tyknZ2d@comnet...
Is it really true that MFC requires that your code supports the TCHAR
mechanism?
Just for example:
CWnd::SetWindowText has the following prototype:
http://msdn2.microsoft.com/en-us/library/yhczy8bz(VS.80).aspx
void SetWindowText(
LPCTSTR lpszString
);
As you can read, the string parameter is of type LPCTSTR, which means 'const
TCHAR *'.
If you have code written e.g. in VC6 or VC7.1 (aka VS2003) that uses
std::string, you may have something like this:
std::string title;
title = "Something...";
...
MyWindow->SetWindowText( title.c_str() );
This code would compile fine in VC6 or VC7.1, where the default character
model is MBCS/ANSI (i.e. TCHAR expands to char, so LPCTSTR expands to const
char*, so std::string::c_str() matches the parameter type of
CWnd::SetWindowText()).
But if you try to compile the same code with VS2005 or VS2008, you will have
compiler errors, because the default character model for VS2005/2008 is
Unicode, so TCHAR expands to wchar_t, so LPCTSTR expands to const wchar_t *,
CWnd::SetWindowText wants a 'const wchar_t*', and that does *not* match with
std::string::c_str() (which returns 'const char*').
So you should replace by hand all occurrences of std::string with
std::wstring (and use proper string literal decorations: L"" or better
_T("")).
This is why I suggested using the TCHAR-model with std::basic_string in my
previous post.
And this is one of the reasons I like CString better than std::string for
MFC programming.
There are also other reasons, for example CWnd::GetWindowText has a useful
overload with CString reference:
void GetWindowText(
CString& rString
) const;
not available for std::string/wstring.
However, I would like to point out that this is just IMHO.
Other programmers use or like std::string/wstring better, I respect them.
I just have my own ideas.
Giovanni