المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Is CString really based on TCHAR?



C++ Programming
07-28-2011, 07:00 PM
Hi.

I wrote a simiral question before but I feel like I am still confused.

I am compiling in Unicode and I know that in Unicode circumstance CString is allocated with TCHAR as you see below.

typedef ATL::CStringT<span class="code-keyword"></span TCHAR, StrTraitMFC_DLLspan class="code-keyword"/span TCHAR > CString;
So obviously CString has nothing to do with LPCTSTR, which is const char *, or any other type, but TCHAR.
However, when I try to do this code below it occurs an error.

CString _str = TEXT("ABCD");TCHAR* _tstr = _str;
The correct version of the code above should be like this.

CString _str = TEXT("ABCD");TCHAR* _tstr = (TCHAR*)(LPCTSTR)_str;

There are two type castings, to LPCTSTR and then to TCHAR*, even though CString is defined with TCHAR type.

I'd like to know why this weird conversion happens from the operator overloading's view of CStringT, and CSimpleStringT and also the template structure of all the classes related to CString.

If I am asking too much to answer in a few sentences, any web pages' link that explain this will be appreciated.

Thanks in advance!