C ++ string full guide - STL and ATL classes

xiaoxiao2021-03-06  74

C string Complete Guide (2) - STL and ATL class author: Ripple Category: VC / VC.NET Date: 2003-1-6 14:31:59

C string full guide (2) - STL and ATL class STL class STL only one string class, that is, Basic_String. Basic_string manages a zero-ended character array. The character type is determined by the template parameter. Typically, Basic_String is processed as an opaque object. A read-only pointer can be obtained to access the buffer, but the write operation is made by the membersic_string member function. Basic_string predefined two special cases: String, containing a Char type character; Which, contains Wchar_t type characters. Without built-in TCHAR special case, you can implement the following code: // Specified Typedef Basic_String Tstring; // Tchar String // Construct String Str = "CHAR STRING"; // Construct WString WStrding from LPCSTR = L "Wide Char String "; // Structure TString TSTR = _T (" tchar string ") from LPCWSTR; // From LPCTSTR Structure // Data Extraction LPCSTR PSZ = Str.c_Str (); // Pointing the read-only pointer LPCWSTR PWSZ = WSTR from the STR buffer .c_str (); // pointing to the read-only pointer LPCTSTSTSTR PTTSZ = TSTR.C_STR () to the WSTR buffer LPCTSTR (); / / pointing to the read-only pointer to the TSTR buffer and _BSTR_T, Basic_String cannot be converted between character sets. However, if a constructor accepts the corresponding character type, the pointer returned by c_str () can be passed to this constructor. For example: // From Basic_String Construction_BSTR_T_STR_T BS1 = Str.c_STR (); // From LPCSTR_T_BSTR_T BS2 = WSTR.C_STR (); // From LPCWSTR_TATL CCOMBSTRCCMBSTR is ATL's BSTR Package. Some cases are more useful than _BSTR_T. The most important thing is that CCOMBSTR allows operations to implicit BSTR. That is to say, the CCOMBSTR object automatically manages BSTR memory when passing a CCOMBSTR object to the CIC method. For example, to call the following interface functions: // Simple interface strunt istuff: public iunknown {// COM program ... stdmethod (STDMETEXT); stdmethod (BSTR * PBSText);}; ccombstr There is a BSTR operation method that can pass BSTR directly to setText (). There is also an Operator & method that returns BSTR *, passes BSTR * to the relevant functions that require it. CCOMBSTR BS1; CCOMBSTR BS2 = "New TEXT"; PSTUFF-> GetText (& BS1); // OK, acquire internal BSTR address PSTUFF-> setText (BS2); // OK, call BSTR conversion PSTUFF-> setText ((BSTR) BS2); // CAST OK, the same top [ccombstr] CCOMBSTR has a constructor similar to _bstr_t. But there is no built-in converter to build MBCS strings. You can call the ATL macro for conversion.

// Construct CCOMBSTR BS1 = "char string"; // Structure CCOMBSTR BS2 = L "Wide char string" from LPCSTR; // Structure CCOMBSTR BS3 = BS1; // Copy CCOMBSTRCCMBSTR BS4; BS4.LOADSTRING (IDS_SOME_STR); // / Load // data extraction from string table // Data extraction BSTR BSTR1 = BS1; // Return to internal BSTR, but cannot be modified! BSTR BSTR2 = (BSTR) BS1; // Cast OK, the same BSTR BSTR3 = bs1.copy (); // Copy BS1, return BSTRBSTR BSTR4; BSTR4 = BS1.DetACH (); // BS1 no longer manages its BSTR / / ... sysfreestring (bstr3); sysfreeestring (bSTR4); The last example above is used to DETACH () method. After the method is called, the CCOMBSTR object will no longer manage its BSTR or its corresponding memory. So BSTR4 must call sysfreestring (). Finally, discuss the reference operator (Operator &). Its transcendence makes some STL sets (such as LIST) can't use CCOMBSTR directly. Returns a pointer to the tolerance class using a reference operation on a collection. However, use the reference operation on CCOMBSTR, returning BSTR *, not ccombstr *. However, you can use ATL's CADAPT class to solve this problem. For example, to create a CCOMBSTR queue, you can declare: std :: list > BSTR_List; CADAPT provides a collection of operations required for the collection, which is implied in code. At this time, use BSTR_LIST like a CCOMBSTR queue. CComvariantccomvariant is a Package class of Variant. But unlike _variant_t, its Variant is not implicit, you can directly operate the Variant member in the class. CCOMVARIANT offers a variety of constructor and multiple types of operations. Here only describes the operations related to strings. // Construct ccomvariant v1 = "char string"; // Structure Ccomvariant V2 = L "wide char string" from LPCSTR; // Structure CCOMBSTR BS1 = "BSTR BOB" from LPCWSTR; CCOMVARIANT V3 = (BSTR) BS1; // BSTR copy // Data extraction CCOMBSTR BS2 = v1.bstrval; // Extract BSTR with _Variant_t from Variant_t, CCOMVARIANT does not have a conversion operation between different Variant types. Variant members must be manipulated directly and determine the type of Variant. Call the ChangeType () method to convert CCOMVARIANT data to BSTR. CCOMVARIANT V4 = ... // initializes V4CCOMBSTR BS3 from some type; if (ac_bhangetype (v4.changetype (vt_bstr))) BS3 = V4.BSTRVAL; like _variant_t, CCOMVARIANT cannot be directly converted to an MBCS string. To create a transition-only _bstr_t variable, use other class functions that convert Unicode to MBCs, or ATL conversion macros to convert. ATL conversion macro ATL string conversion macro can easily convert different encoded characters and is very effective in functions.

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

New Post(0)