Light use of CSTRING

xiaoxiao2021-03-06  43

The CString class is powerful, and it has not been there. When the STRING class of STL is, it will be strong when using cstring.

The function is attracted. However, due to the inside of its internal mechanism, the novice is converted when converting CSTRING's character array

It is easy to have a lot of problems. Because CString has been overloaded with the LPCTSTR operator, the CString class is constant to const.

CHAR * There is no trouble when converting, as shown below:

CHAR A [100]; CSTRING STR ("aaaaaaa"); STRNCPY (a, (lpctstr) STR, SIZEOF (A)); or as follows: Strncpy (A, STR, SIZEOF (A)); the above two types Correct. Because Strncpy's second parameter type is const char *. So the compiler

The CString class will be automatically converted into const char *. Many people are confused about LPCTSTR, let us

come and see:

1.LP represents long pointers, the difference between Win16 has long pointers (LP) and short pointer (P), and there is no difference in Win32

It is 32 bits. So the LP and P here are equivalent.

2.c Represents Const

3. T, we know that Tchar is Wchar_T when using Unicode mode, compiled into char.

Then you can see that LPCTSTR (PCTSTR) is const wchar_t *, pcwstr, lpcwstr, in Uincode.

Multi-byte character mode is const char *, pcstr, lpcstr.

Next, we look at the case of unicode, how to convert CString into char *, many beginners are convenient

Use the following method:

Is this right? We first look at an example:

CString Str ("aa"); str, "aaaaaaaa"); cout << (lpctstr) str << Endl; Exceeding is abnormal in Debug, we all know that there is CSTRING class Your own character pointer, pointing to a divided

The character buffer. If the number of characters written in the inside beyond the buffer range, there is of course abnormalities. But this process

The order does not have problems under the Release version. It turned out that the CSTRING class has been optimized. When you need to allocate memory

When less than 64 bytes, directly assign 64 bytes of memory, so that the size of the general CString class character buffer is

64, 128, 256, 512 ... This is to reduce the number of memory allocations and increase the speed.

That someone said that the number of characters written in the inside did not exceed its original character, and won't be wrong, such as

CSTRING STR ("Aaaaaaaa"); STRCPY ((Char *) (LPCTSTR) STR, "AA"); cout << (lpctstr) str << Endl; this seems to be no problem. Let's look at the following example:

CString Str ("aaaaaaaa"); STRCPY ((Char *) (LPCTSTR) STR, "AA"); cout << (lpctstr) str-< endl; cout << str.getLength () << Endl; we see The length of the STR does not change, continues to be 7 instead of 2. There is also a more serious problem:

CSTRING STR ("Aaaaaaaa"); CString str1 = STR; STRCPY ((char *) (lpctstr) STR, "aa"); cout << (lpctstr) str << endl; cout << (lpctstr) str1 << Endl Say, we only change STR, STR1 should have no change, but they have become "AA" when they are facts. Is Str and

The buffer pointer inside STR1 is one. We know in Effective C if your class

There is a pointer inside, please write a copy constructor and assignment operator for your class. Do not let the pointer inside the object points to the same area, but it should be reassigned. Is it wrong with Microsoft?

It turns out that there is a concept of "writing" and "reference count". The use of CSTRING class is very wide, so it is possible

A large amount of CString temporary object is generated inside the system. At this time, in order to optimize efficiency, it is wide in the system software.

Wide-use "write time copy" concept. That is, when another CString is generated from a cstring, it does not copy its character.

The contents of the rush area, but only the "reference count" of the character buffer 1. When you need to overwrite the content within the character buffer,

Assign memory and copy the content. I will give an example of "write time copy" and "reference count".

We return to the topic, when we need to convert CString into char *, what should we do? Just

Trouble, as shown below:

CString Str ("AAAAAAAA"); STRCPY (Str.getBuffer (10), "AA"); str.releaseBuffer (); When we need to call GetBuffer (int N) while we need a character array, where n is the array of characters we need. Length.

Be sure to call ReleaseBuffer immediately after delivery.

It is also important to use CHAR * where you can use const char *.

转载请注明原文地址:https://www.9cbs.com/read-86889.html

New Post(0)