CString::CString

These constructors initialize 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. Some of these constructors act as conversion functions. This allows you to substitute, for example, an LPTSTR where a CString object is expected.

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

CString( ); 

CString(
const CString& stringSrc );

CString(
TCHAR ch,
int nRepeat = 1 );

CString(
LPCSTR lpsz ); 

CString(
LPCWSTR lpsz );

CString(
LPCTSTR lpch,
int nLength );

CString(
const unsigned char* psz ); 

Parameters

  • stringSrc
    Specifies an existing CString object to be copied into this CString object.
  • ch
    Specifies a single character to be repeated nRepeat times.
  • nRepeat
    Specifies the repeat count for ch.
  • lpch
    Specifies a pointer to an array of characters of length nLength, not null-terminated.
  • nLength
    Specifies a count of the number of characters in lpch.
  • psz
    Specifies a null-terminated string to be copied into this CString object.
  • lpsz
    Specifies a null-terminated string to be copied into this CString object.

Remarks

Several forms of the constructor have special purposes:

  • CString( LPCSTR lpsz )   Constructs a Unicode CString from an ANSI string.
  • CString( LPCTSTR lpsz )   You can use this constructor to load a string 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.

Example

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((LPCTSTR)ID_FILE_NEW); // s7 = "Create a new document"

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

Requirements

  Windows CE versions: 1.0 and later
  Header file: Declared in Afx.h
  Platform: H/PC Pro, Palm-size PC, Pocket PC

See Also

CString::operator =