Re: string vs string*
* Giovanni Dicanio:
"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
Except for the "by hand" :), I concur.
(and use proper string literal decorations: L""
Yep.
or better _T("")).
But this, is objectively not meaningful: writing more in order to indicate
portability that isn't true and make the code less portable.
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.
std::basic_string<> is an ugly beast indeed, but CString is perhaps even uglier. :-)
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.
When in Rome, do as the Romans... All this convenience stuff can be easily
implemented as small wrappers. But that gets tedious fast.
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.
Any proper string class runs circles around std::basic_string. But CString
isn't it. Except for convenience in MFC/ATL.
Cheers,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?