CString Semantics

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at CString Semantics.

Even though CString objects are dynamic objects that can grow, they act like built-in primitive types and simple classes. Each CString object represents a unique value. CString objects should be thought of as the actual strings rather than as pointers to strings.

You can assign one CString object to another. However, when you modify one of the two CString objects, the other CString object is not modified, as shown by the following example:

   CString s1, s2;
   s1 = s2 = _T("hi there");

   ASSERT(s1 == s2); // they are equal

   s1.MakeUpper();  // Does not modify s2
   ASSERT(s2[0] == _T('h')); // s2 is still "hi there"   

Note in the example that the two CString objects are considered "equal" because they represent the same character string. The CString class overloads the equality operator (==) to compare two CString objects based on their value (contents) rather than their identity (address).

Strings (ATL/MFC)

Show: