CString (MFC)

A common lament seen in various newsgroups deals with the Standard string class as opposed to the Microsoft Foundation Class called CString. Often programmers realize that a standard portable answer is better than a proprietary nonportable one, but in porting their application from a Win32 platform, they discover that they are relying on special functions offered by the CString class.

Things are not as bad as they seem. In this message, Joe Buck points out a few very important things:

The old libg++ library had a function called form(), which did much the same thing. But for a Standard solution, you should use the stringstream classes. These are the bridge between the iostream hierarchy and the string class, and they operate with regular streams seamlessly because they inherit from the iostream hierarchy. An quick example:

   #include <iostream>
   #include <string>
   #include <sstream>

   string f (string& incoming)     // incoming is "foo  N"
   {
       istringstream   incoming_stream(incoming);
       string          the_word;
       int             the_number;

       incoming_stream >> the_word        // extract "foo"
                       >> the_number;     // extract N

       ostringstream   output_stream;
       output_stream << "The word was " << the_word
                     << " and 3*N was " << (3*the_number);

       return output_stream.str();
   } 

A serious problem with CString is a design bug in its memory allocation. Specifically, quoting from that same message:

   CString suffers from a common programming error that results in
   poor performance.  Consider the following code:
   
   CString n_copies_of (const CString& foo, unsigned n)
   {
           CString tmp;
           for (unsigned i = 0; i < n; i++)
                   tmp += foo;
           return tmp;
   }
   
   This function is O(n^2), not O(n).  The reason is that each +=
   causes a reallocation and copy of the existing string.  Microsoft
   applications are full of this kind of thing (quadratic performance
   on tasks that can be done in linear time) -- on the other hand,
   we should be thankful, as it's created such a big market for high-end
   ix86 hardware. :-)
   
   If you replace CString with string in the above function, the
   performance is O(n).
   

Joe Buck also pointed out some other things to keep in mind when comparing CString and the Standard string class: