In C, it indicates a string. It does not have the string concept. Only char * and char [] can be used. In MFC, to serve string operations, a class CString is added, the header file of this class is afx. h.
The conversion from char * To CString is simple. You only need to use the CString constructor.
This document describes how to convert CString to const char * and char.
The CString class is more powerful than the string class of STL. when new users use CString, they will be attracted by its powerful functions. however, due to lack of knowledge about its internal mechanism, it is easy for beginners to encounter many problems when converting a CString to a C character array. because the CString operator is already overloaded, it is not difficult to convert the CString class to const char *, as shown below:
Char a [100];
CString str ("aaaaaa ");
Strncpy (a, (LPCTSTR) str, sizeof ());
Or:
Strncpy (a, str, sizeof (a); flyhorse Note: The above statement sets the CString --> const char *
Both methods are correct. because strncpy's second parameter type is const char *. therefore, the compiler automatically converts the CString class to const char *. many people are confused about what the LPCTSTR is. Let's take a look:
1. LP indicates a long pointer. In Win16, there is a difference between a long pointer (LP) and a short pointer (P). In Win32, there is no difference, both 32-bit. therefore, the LP and P here are equivalent.
2. c Indicates const
3. t: What is it? We know that tchar is wchar_t when it is compiled in Unicode mode. If it is compiled into char in general mode, we can see that the length of lpctstr (pctstr) is const wchar_t * In uincode *, pcwstr, lpcwstr, Which is const char *, pcstr, and lpcstr in Multi-byte character mode. next we will look at how to convert a cstring to a char * in non-Unicode situations. Many beginners use the following methods for convenience:
(Char *) (lpcstr) Str
Note: If you only convert cstring --> char * without modifying the content of the string, this conversion is acceptable.
Is that true? Let's first look at an example:
Cstring STR ("AA ");
Strcpy (char *) (lpctstr) STR, "aaaaaaaa ");
Cout <(lpctstr) STR <Endl;
An exception occurs when running in debug. We all know that the cstring class has its own character pointer pointing to an allocated character buffer. if the number of characters written to it exceeds the buffer range, an exception may occur. but this program does not have problems in the release version. the original cstring class has been optimized. when the memory to be allocated is smaller than 64 bytes, the 64-byte memory is directly allocated, and so on. Generally, the size of the cstring character buffer is 64,128,256,512... this is to reduce the number of memory allocations and increase the speed.
Then someone will say that the number of characters I write to it does not exceed the original number of characters, so there will be no errors, such
CString str ("aaaaaaa ");
Strcpy (char *) (LPCTSTR) str, "aa ");
Cout <(LPCTSTR) str <endl;
It seems that there is no problem. Let's take a look at the example below:
CString str ("aaaaaaa ");
Strcpy (char *) (LPCTSTR) str, "aa ");
Cout <(LPCTSTR) str <endl;
Cout <str. GetLength () <endl;
We can see that the length of str has not changed, and continues to be 7 instead of 2. There are more serious problems:
CString str ("aaaaaaa ");
CString str1 = str;
Strcpy (char *) (LPCTSTR) str, "aa ");
Cout <(LPCTSTR) str <endl;
Cout <(LPCTSTR) str1 <endl;
We only changed str, and str1 should have not changed, but they all changed to "aa" in real time ". is the character pointer in str and str1 pointing to a buffer zone. we learned in Objective C ++ that if your class contains pointers, please write a copy constructor and a value assignment operator for your class. instead of directing the internal pointer of two objects to the same region, the memory should be re-allocated. is Microsoft making a mistake?
There is also a concept of "Copy at write time" and "reference count. the CString class is widely used, which may generate a large number of temporary CString objects in the system. in order to optimize the efficiency, the "Write-time replication" concept is widely used in the system software. that is, when a CString generates another CString, it does not copy its character buffer content, but adds the "reference count" of the character buffer to 1. when you need to rewrite the content in the character buffer, allocate the memory and copy the content. in the future, I will give an example of "Copy at write" and "reference count". We will return to the topic. What should we do when we need to convert CString to char? At that time, it was only a bit of trouble, as shown below:
CString str ("aaaaaaa ");
Strcpy (str. GetBuffer (10), "aa ");
Str. ReleaseBuffer ();
Note: Change CString --> char * and modify the string content.
When we need a character array, call GetBuffer (int n), where n is the length of the character array we need. call ReleaseBuffer () immediately after use. It is also important to avoid using char * Where const char * can be used *
Article Source: feino Network (www.firnow.com): http://dev.firnow.com/course/3_program/c++/cppjs/2008821/136752.html