CString::CString

CString();

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

CString(TCHARch**,intnRepeat=1);**
throw(CMemoryException);

CString(LPCTSTRlpch**,intnLength);**
throw(CMemoryException);

CString(constunsignedchar*psz);
throw(CMemoryException);

CString(LPCWSTRlpsz**);**
throw(CMemoryException);

CString(LPCSTRlpsz**);**
throw(CMemoryException);

Parameters

stringSrc

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

ch

A single character to be repeated nRepeat times.

nRepeat

The repeat count for ch.

lpch

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

nLength

A count of the number of characters in pch.

psz

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

lpsz

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

Remarks

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( LPCSTRlpsz**)**   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( LPCWSTRlpsz**)**   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

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

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

CString OverviewClass MembersHierarchy Chart

See Also   CString::operator =