C ++ string full guide (2) - STL and ATL classes

zhaozj2021-02-16  72

C string full guide (2) - STL and ATL classes

Author: Translation: even wave Thursday, November 21 2002 3:53 PM STL STL class has only one string class that 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:

// Specification Typedef Basic_String Tstring; // Tchar String // Constructs String Str = "Char String"; // Construct WString WStr = L "Wide Char String" from LPCSTR; // Tstring Tstring Tstring Tstring Tstring TSTR = _t (" Tchar string "); // From LPCTSTR Structure // Data Extraction LPCSTR PSZ = Str.c_STR (); // Pointing the read-only pointer lpcwstr pwsz = wstr.c_str pwsz = wstr.c_str (); // pointing to WSTR buffers only Read pointer LPCTSTR PTSZ = TSTR.C_STR (); // Point read-only pointer to the TSTR buffer

Unlike _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. E.g:

// From Basic_String Construction_BSTR_T_BSTR_T BS1 = Str.c_Str (); // From LPCSTR_T_BSTR_T BS2 = WSTR.C_STR (); // From LPCWSTR Structure_T

ATL CCOMBSTRCCMBSTR is the BSTR packaging class of ATL. 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 Struct ISTUFF: Public IUNKNOWN {// COM program ... stdmethod (BSTR BSTEXT); stdmethod (BSTR * PBSText);

CCOMBSTR has 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

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 the operation 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 from Variant

Unlike _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 converted directly into a 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.

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

New Post(0)