Friday, April 28, 2006

Finding correlations between the CString Family and the Standard String Family

If we were to draw a correlation between the CString Family and Standard String Classes, we would notice -
  1. CStringT as the equivalent of std::basic_string
  2. CStringA as the equivalent of std::string
  3. CStringW as the equivalent of std::wstring
However, the standard library itself doesn't contain a "CString" equivalent - one that can at best be - std::basic_string <TCHAR>

This makes CString (VS 7.x +) "cool".

The history of CString...

CString really wasn't always as likeable as it is today. If one were to compare the CString of Visual Studio 7.x and better with the class supplied by MSVC 6.0, one might not be wrong in judging the erstwhile version as relatively inflexible, and restrictive.

What made CString of yesterday "inflexible"?

1.Only for MFC-folks!

Yes... CString with MSVC 6.0 was notoriously connected to MFC Projects as if other project types didn't have any need for a TCHAR String Wrapper Class! One reason for it would be the linker error one would get for using this class in a non-MFC Project.

2. Need to use a CString? Convert to TCHAR!

CString is a string wrapper for TCHAR strings. In doing so, it came with a good number of helpful member methods. However, the implementation of this class in MSVC 6.0 was such that if a programmer needed to make use of the utility member methods, he would need to convert his string data type to TCHAR - back and forth.

This "restriction" automatically excluded programmers using ANSI strings (char*) in UNICODE builds from using this class, and ditto for those using UNICODE strings (WCHAR*) in non-UNICODE builds from using this class - as in both these cases, the strings in question aren't TCHAR strings - the type that CString wraps.

What makes CString of today "flexible" and actually more "likeable"?

1. Use it in any project type - even in a console application!

Yes... All the programmer needs to do is to include the following header -
#include <atlstr.h>
2. Don't change your String Type to suit CString! Choose the right CString instead!

With Visual Studio 7.x and better, CString is actually a specialization of template Class CStringT for type T as TCHAR.

Additionally, there exist two useful specializations of CStringT -
  1. CStringA that specializes CStringT for type char, and
  2. CStringW that specilizes CStringT for type wchar_t.
Hence, CString is actually CStringA for a non-UNICODE build and is a CStringW for a UNICODE build.

The existence of CStringA and CStringW gives the programmer, the flexibility to use the utility member methods supplied by CStringT even when their strings aren't of type - TCHAR.

The rationale behind this FAQ...

In the past few years of answering questions aplenty on Codeguru's Visual C++ Forum, I have realized the following three things -

  1. CString counts amongst the most popularly used string classes - ever.
  2. People keep asking questions that have been answered many times over, but answers to which are not easily locatable.
  3. (1) and (2) are going to continue being this way for some time now.

So, me thinks - one centralized source of information on CString and related classes will help - someone, sometime!

Feel free to share feedback on the content you see.
Additionally, if you have a FAQ in mind that I haven't answered here - feel free to request one. ;)

Best Regards,

Siddhartha Rao
Microsoft MVP - Visual C++
Codeguru Community Moderator