共用方式為


Strings: CString Semantics

OverviewHow Do I

Even though objects are dynamically growable objects, 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.

The most obvious consequence of using CString objects as values is that the string contents are copied when you assign one CString to another. Thus, even though two CString objects may represent the same sequence of characters, they do not share those characters. Each CString has its own copy of the character data. When you modify one CString object, the copied CString object is not modified, as shown by the following example:

CString s1, s2;
s1 = s2 = "hi there";

if( s1 == s2 )            // TRUE - they are equal
    ...

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

Notice 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).