Export (0) Print
Expand All

Using CString

Updated: October 2009

The topics in this section describe programming by using CString.

CString, CStringA, and CStringW are Microsoft-specific string classes that are available for general C++ development.

A CStringA object contains string data that is based on the char type, and supports single-byte and multibyte (MBCS) strings. Similarly, a CStringW object contains string data that is based on the wchar_t type, and supports Unicode strings. A CString object supports either the char type or the wchar_t type. Which one it supports depends on which one of the symbols, MBCS or UNICODE, is defined at compile time.

A CString object keeps character data in a CStringData object. CString accepts null-terminated C-style strings, but does not retain the null character in the stored character data. Instead, CString tracks string length. CString does provide a null terminator when it exports a C-style string. You can insert a null character amidst stored character data, but it may produce unexpected results.

CString is used in native projects. For managed-code (C++/CLI) projects, use System::String.

CString, CStringA, and CStringW are defined in atlstr.h and exported from MFC90.dll. CStringT is defined in cstringt.h. The following set of string classes can be used without linking an MFC library, with or without CRT support: CAtlString, CAtlStringA, and CAtlStringW.

The CString, CStringA, and CStringW classes are instances of a class template called CStringT. They take advantage of a template programming technique called specialization. Each is implemented as a typedef based on specializing the template class CStringT for the type of character data they support. They do not define their own methods. Instead, they rely on those defined by CStringT, specialized to work with the character data that they contain.

To add more capabilities than CString, CStringA, or CStringW currently offer, consider creating a subclass of CStringT that contains the additional features. Then create an instance that is similar to CString but is based on the subclass of CStringT.

Basic CString Operations

Describes basic CString operations. This includes how to create objects from C literal strings, access individual characters in a CString, concatenate two objects, and compare CString objects.

String Data Management

Discusses how to use Unicode and MBCS with CString.

CString Semantics

Explains how CString objects are used.

CString Operations Relating to C-Style Strings

Describes how to manipulate the contents of a CString object as a C-style null-terminated string.

Allocating and Releasing Memory for a BSTR

Discusses how to use memory for a BSTR and COM objects.

CString Exception Cleanup

Explains that explicit cleanup in MFC is no longer required.

CString Argument Passing

Explains how to pass CString objects to functions and how to return CString objects from functions.

Unicode and Multibyte Character Set (MBCS) Support

Discusses how MFC is enabled for Unicode and MBCS support.

CStringT Class.

Provides reference information about the shared CStringT class.

Strings (ATL/MFC)

Contains links to topics that describe several ways to manage string data.

Class Template Instantiation

CString is a typedef based on CStringT, an instance of a specialization of a class template.

Date

History

Reason

October 2009

Added a summary to the introduction.

Customer feedback.

Community Additions

ADD
Show:
© 2014 Microsoft