For cstring analysis, "strcmp": the problem that parameter 1 cannot be converted from "cstring" to "const char *"
Cstring csnewlistboxtext;
Cstring csoldlistboxtext (g_csfirstlistboxseltext );
If (strcmp (csnewlistboxtext, csoldlistboxtext )! = 0)
// Release version error:
// Error c2664: "strcmp": parameters 1 and 2 cannot be converted from "cstring" to "const char *"
// Change it to the following: add (char *) (lpctstr) in front ).
If (strcmp (char *) (lpctstr) csnewlistboxtext, (char *) (lpctstr) csoldlistboxtext )! = 0)
// Yes
Online materials:
Cstring Analysis
The cstring class is more powerful than the string class of STL. It is always powerful when new users use cstring.
However, due to lack of understanding about its internal mechanism, when a newbie converts a cstring to a C character array
Many problems may occur. Because the cstring operator is already overloaded, The cstring class
Char * conversion is not troublesome, as shown below:
Char A [100];
Cstring STR ("aaaaaa ");
Strncpy (A, (lpctstr) STR, sizeof ());
// Or as follows:
Strncpy (A, STR, sizeof ());
The above two methods are correct. Because the second parameter type of strncpy is const char *. Therefore, the compiler
The cstring class is automatically converted 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.
So the LP and P here are equivalent.
2. c Indicates const
3. What is t? We know that tchar is wchar_t in Unicode mode and compiled into char in general.
We can see that during uincode, The lpctstr (pctstr) is const wchar_t *, pcwstr, and lpcwstr, in
The multi-byte character mode is const char *, pcstr, and lpcstr.
Next, let's look at how to convert a cstring to a char * in non-Unicode situations. Many beginners aim to make it easier.
// Use the following method:
(Char *) (lpcstr) Str. 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
Character Buffer. If the number of characters written in the buffer exceeds the buffer range, an exception will occur.
In the release version, no problem exists. The cstring class has been optimized. When the memory needs to be allocated
When the size is smaller than the byte, the memory of the byte 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 it continues to be, rather than. 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 in real time they all changed to "AA". Do STR and
The character pointer in str1 points to a buffer. We learned in Objective C ++ that if your class
Contains pointers. Please write a copy constructor and a value assignment operator for your class.
The Pointer Points to the same region and the memory should be re-allocated. Is Microsoft making a mistake?
It turns out that there is also a concept of "Copy at write time" and "reference count". The cstring class is widely used, so it is possible
A large number of temporary cstring objects are generated in the system. In this case, to optimize the efficiency, it is widely used in the system software.
The general concept of "Copy at write" is that when a cstring is generated from another cstring, its character is not copied.
Only adds the "reference count" of the character buffer zone. Only when the content in the character buffer zone needs to be rewritten
Allocate memory and copy the content. In the future, I will give an example of "Copy at write time" and "reference count ".
Let's go back to the topic. What should we do when we need to convert cstring to char? Only
// A little bit of trouble, as shown below:
Cstring STR ("aaaaaaa ");
Strcpy (Str. getbuffer (10), "AA ");
Str. releasebuffer ();
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 that you do not use char * Where const char * can be used *
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/cobay/archive/2008/12/19/3556307.aspx