THE CString CLASS

You used a CString object in the previous section, and there’s more to it than you have seen so far. The CString class provides a convenient and easy-to-use mechanism for handling strings that you can use just about anywhere a string is required. To be more precise, you can use a CString object in place of strings of type const char*, or of type LPCTSTR, which is a type that comes up in Windows API functions. If you are using the DDX mechanism for strings, then you must use CString, otherwise DDX won’t work.

The CString class provides the overloaded operators shown in the following table.

OPERATOR USAGE
= Copies one string to another, as in:str1 = str2;str1 = _T("A normal string");
+ Concatenates two or more strings, as in:str1 = str2 + str3 + _T(" more");
+= Appends a string to an existing CString object, as in:str1 += str2;
== Compares two strings for equality, as in:if(str1 == str2) // do something...
< Tests if one string is less than another.
<= Tests if one string is less than or equal to another.
> Tests if one string is greater than another.
>= Tests if one string is greater than or equal to another.

The variables str1 and str2 in the preceding table are CString objects.

CString objects automatically grow as necessary, such as when you add an additional string to the end of an existing object. For example:

CString str = _T("A fool and your money");
str += _T("are soon partners.");

The first statement declares and initializes the ...

Get Ivor Horton's Beginning Visual C++ 2012 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.