一、TCHAR 和_T
#ifdef _UNICODE
typedef wchar_t TCHAR;
#define __T(x) L ## x
#else
typedef char TCHAR;
#define __T(x) x
#endif
当你在工程设置中选择 Unicode 字符集时,编译器会用 _UNICODE 定义进行编译。如果你选择MBCS(多字节字符集),则编译器将不会带 _UNICODE 定义 。一切取决于_UNICODE 的值。同样,每一个使用字符指针的 Windows API 函数会有一个 A(ASCII) 和一个 W(Wide/Unicode) 版本,这些版本的 实际定义也是根据 _UNICODE 的值来决定:
二、封装tstring
tstring 是基于string 和wstring 用typedef来定义的,如下:
#ifdef _UNICODE
typedef wstring tstring;
#else
typedef string tstring;
#endif
一旦定义了 tstring,便可以像下面这样编码:
tstring s = _T("Hello, world");
三、_tprintf 和 _tscanf
同样,_tprintf 和 _tscanf 对应于 printf 和 scanf。所有带"t"的版本使用 TCHARs 取代了chars。那么怎样把以上的这些应用到 std::string 上呢?很简单。STL已经定义了string和wstring,基于string 和wstring定义一个tstring。