Talking about the use of CString
Author:
The CString class is powerful, which has no difference than STL's String class. When using cstring, it will be attracted by its powerful function. However, due to the internal mechanism of it, novice is converting CSTRING's character arrays to C It is easy to have a lot of problems. Because CString has been overloaded with the LPCTSTR operator, there is no trouble when the CString class is converted to constchar *, as shown below: Char a [100]; CSTRING STR ("aaaaaaaa); strncpy (A, (LPCTSTR) STR, SIZEOF (A)); or as follows: STRNCPY (A, STR, SIZEOF (A)); the above two usage are correct. Because Strncpy's second parameter type is const char *. So compile The CSTRING class will automatically convert the CString class into const char *. Many people are confused about LPCTSTR, let's take a look: 1.LP means long pointers, there is a long pointer (LP) and short pointer under Win16 (P The difference is the difference in Win32, which is 32 bits. So the LP and P are equivalent .2.c said that const3.t is something, we know that tchar is compiled with Unicode mode. It is Wchar_T. It can be seen when compiled as a char to Char in normal time. It is Const Wchar_T *, PCWSTR, LPCWSTR, which is const char *, pcstr, lpcstr, next to multi-byte character mode. In the case of non-Unicode, how to convert CString into char *, many beginners have the following methods: (char *) (lpcstr) str. Is this right? We first look at an example: CString Str ("AA" ); strcpy (lpctstr) STR, "aaaaaaaaa"); cout << (lpctstr) str << Endl; Exceeding an exception in DEBUG, we all know that there is its own character pointer inside the CSTRING class, pointing An allocated character buffer. If the number of characters written in the inside beyond the buffer range, there will be an exception. But this program does not have problems under the Release version. It turned out that the CSTRING class has been optimized. When needed When the memory is less than 64 bytes, the 64-byte memory is allocated directly. In this class, the size of the general CString class character buffer is 64, 128, 256, 512 ... This is to reduce the number of memory allocations, improve the speed. Someone said me Written inside The number of characters does not exceed its original characters, and it will not be wrong, such as CString Str ("Aaaaaaaaaa); STRCPY ((Char *) (LPCTSTSTR) STR," AA "); COUT << (LPCTSTSTR) STR <