Re: Using <string> in Unicode
"Ben Menashe" <benm5678@gmail.com> schrieb im Newsbeitrag
news:18tch.57$C76.49@newsfe02.lga...
Hi,
I have an existing project compiled in multibyte. I need to convert to
Unicode. What's a good way to get every string object to use wstring ?
I tried: #define string wstring
I guess it works in a simple test program, but in my actual project code
there's issues... in some places it complains about not being able to
convert from const char* to const wchar* , when i debug it looks like
c_str() is returning const char* instead but the string object shows to be
a wstring.... strange.
Is this the proper way ? Is there some good article that shows how to
using <string> in Unicode ? I searched and couldn't find.
Don't use #define to change the meaning of an existing type. Even if you'd
put the #define at a place where all occurences of string, even those in the
string header file, are re-defined as wstring, it probably wouldn't work. At
least you would get multiple definitions of wstring.
If you just want to change your program to Unicode, replace all occurences
to sting in your code with wstring, replace char by wchar_t and char string
literals ("...") with wchar_t string literals (L"..."), and call wide
character versions of all functions accepting or returning strings or chars.
But after that change, there is no easy way back.
A better solution, which allows you to switch characterset by just selecting
an MBCS or Unicode build ist to define your own string type:
typedef std::basic_string<TCHAR> tstring;
Then replace string by tstring, replace "..." by _T("...") and, except for
functions of the Windows API or MFC, which already use TCHAR instead of
char, use generic string functions like _tcslen instead of strlen.
But whatever you do, there is no easy way...
HTH
Heinz