C ++ string full guide - MFC class

xiaoxiao2021-03-06  69

C string Complete Guide (2) - MFC class author: Ripple Category: VC / VC.NET Date: 2003-1-6 14:29:21

C string Full Guide (2) - MFC class MFC class CStringMFC CString contains TCHAR, which depends on the setting of the pre-process tag. Typically, the CString is like a STL string is an opaque object, and can only be modified by a cstring method. CString is more superior to the STL string that its constructor accepts MBCS and Unicode strings. And can be converted to LPCTSTR, so the CString object can be directly delivered to the function of receiving LPCTSTR, and the c_str () method is not required. // Construct CString S1 = "CHAR STRING"; // Structure CString S2 = L "Wide Char String" from LPCSTR; // Constructs CString S3 ('', 100) from LPCWSTR; // Prepare 100 bytes, fill space CString S4 = "new window text"; // You can use CString: setWindowText (HWNDSOMEWINDOW, S4); // or, Explicitly Mandatory Type Conversion: SetWindowText (HWndSomewindow, (LPCTSTSTSTSTSTSTSTS); String table load string. CString constructs objects through LoadString (). Use the format () method to selectively read a string of a certain format from a string table. // Structure / Load CString S5 ((LPCTSTR) IDS_SOME_STR) from the string table; // load the CString S6, S7; // from the string table (IDS_SOME_STR); // From the string table Load the string S7.Format (IDS_SOME_FORMAT, "BOB", nsomestuff, ...); the first constructor looks a bit blame, but it is indeed a string load method of the document calibration. Note that CString only allows for a mandatory type conversion, that is, forced to convert to LPCTSTR. Forced to convert to LPTSTR (very measured pointer) is wrong. According to old habits, convert CString to lPTSTR can only hurt themselves. Sometimes didn't find an error in the program, it just happened. The correct way to convert to a very quantity pointer is to call the getBuffer () method. The following conventional queue-added elements Take an example how to use CString: CString str = _t ("new text"); lvitem item = {0}; item.mask = 1; item.psztext = (LPTSTR (Lpctstr) str; // is wrong! Item.psztext = str.getBuffer (0); // correct listview_setitem (& item); str.releasebuffer (); // Return the queue to StrpszText membership is lptstr, a very amount of pointer, so use STR's getBuffer (). The parameters of GetBuffer () are the minimum buffers allocated by CString. If you want to assign a 1K TCHAR, call GetBuffer (1024). The parameter is 0, and only the pointer to the string is returned. The error statement on the above example can be compiled, even can work properly, if it is this type. But this does not prove the syntax correct. Perform a very quite compulsory type conversion, break the object-oriented package principle and pass over the internal operation of CString.

If you habits such a forced type conversion, I will eventually encounter an error, but you may not know where you are, because you are doing such a conversion, and the code can run. Know why people always complain about defective software? Incorrect code is breed in the bug. However, you are willing to prepare a known code to make the bug organically multiplied? Still spend some time to learn CString's correct usage makes your code 100% correct. CString has two functions to get BSTR from CString and convert to Unicode if necessary. That is allocsystring () and setsystring (). In addition to setsysString (), both use the BSTR * parameters. // Convert to BSTRCSTRING S5 = "Bob!"; BSTR BS1 = NULL, BS2 = NULL; BS1 = S5.Allocsystring (); S5.Setsystring (& BS2); // ... sysfreestring (BS1); sysfreestring (BS2) Colevariant is very similar to CComvariant. Colevariant inherits to Variant, which can be passed to a function that needs Variant. However, unlike CComvariant, Colevariant has only one LPCTSTR constructor that does not provide a separate LPCSTR and LPCWSTR constructor. In most cases, there is no problem, because it is always willing to treat the string to LPCTSTR. But you have to know this. Colevariant also has a constructor that accepts CString. // Construct CString S1 = _T ("tchar string"); Colevariant V1 = _T ("bob"); // From LPCTSTRIANT V2 = S1; // From CString Copy For CCOMVARIANT, you must directly process Variant members, use ChangeType () The method is converted into a string when necessary. However, Colevariant :: ChangeType () will throw an exception when the conversion fails, not the error code to return HRESULT. // Data extraction Colevariant V3 = ...; // Constructs V3BSTR BS = NULL from some type of type; try {v3.changtype (vt_bstr); BS = v3.bstrval;} catch (ColeException * e) {// error, Unable to convert} sysfreeestring (bs);

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

New Post(0)