Visual Studio 6.0


CString( );

CString( const CString& stringSrc );
throw( CMemoryException );

CString( TCHAR ch, int nRepeat = 1 );
throw( CMemoryException );

CString( LPCTSTR lpch, int nLength );
throw( CMemoryException );

CString( const unsigned char* psz );
throw( CMemoryException );

CString( LPCWSTR lpsz );
throw( CMemoryException );

CString( LPCSTR lpsz );
throw( CMemoryException );



An existing CString object to be copied into this CString object.


A single character to be repeated nRepeat times.


The repeat count for ch.


A pointer to an array of characters of length nLength, not null-terminated.


A count of the number of characters in pch.


A null-terminated string to be copied into this CString object.


A null-terminated string to be copied into this CString object.


Each of these constructors initializes a new CString object with the specified data.

Because the constructors copy the input data into new allocated storage, you should be aware that memory exceptions may result. Note that some of these constructors act as conversion functions. This allows you to substitute, for example, an LPTSTR where a CString object is expected.

Several forms of the constructor have special purposes:

  • CString( LPCSTR lpsz )   Constructs a Unicode CString from an ANSI string. You can also use this constructor to load a string resource as shown in the example below.

  • CString( LPCWSTR lpsz )   Constructs a CString from a Unicode string.

  • CString( const unsigned char* psz )   Allows you to construct a CString from a pointer to unsigned char.

For more information, see in Visual C++ Programmer’s Guide


The following example demonstrates the use of CString::CString.

// example for CString::CString
CString s1;                    // Empty string
CString s2( "cat" );           // From a C string literal
CString s3 = s2;               // Copy constructor
CString s4( s2 + " " + s3 );   // From a string expression

CString s5( 'x' );             // s5 = "x"
CString s6( 'x', 6 );          // s6 = "xxxxxx"

CString s7((LPCSTR)ID_FILE_NEW); // s7 = "Create a new document"

CString city = "Philadelphia"; // NOT the assignment operator

CString OverviewClass MembersHierarchy Chart

See Also   CString::operator =