A CString object consists of a variable-length sequence of characters. The CString class provides a variety of functions and operators that manipulate CString objects using a syntax similar to that of Basic. Concatenation and comparison operators, together with simplified memory management, make CString objects easier to use than ordinary character arrays. The increased processing overhead is not significant. The CString “Application Notes” section (starting on page 598) offers useful information on:
CString Exception Cleanup
CString Argument Passing
The maximum size of a CString object is MAXINT (32,767) characters.
The const char* operator gives direct access to the characters in a CString object, which makes it look like a C-language character array. Unlike a character array, however, the CString class has a built-in memory-allocation capability. This allows string objects to grow as a result of concatenation operations.
No attempt is made to “fold” CString objects (if you make two CString objects containing Chicago, for example, the characters in Chicago are stored in two places).
The CString class is not implemented as a Microsoft Foundation collection class, although CString objects can certainly be stored as elements in collections.
The overloaded const char* conversion operator allows CString objects to be freely substituted for character pointers in function calls. The CString( const char* psz ) constructor allows character pointers to be substituted for CString objects.
Use the GetBuffer and ReleaseBuffer member functions when you need to directly access a CString as a nonconstant pointer to char (char* instead of a const char*).
CString objects follow “value semantics.” A CString object represents a unique value. Think of a CString as an actual string not as a pointer to a string.
Where possible, allocate CString objects on the frame rather than on the heap. This saves memory and simplifies parameter passing.
#include <afx.h>