Here is another one:
LPCTSTR cpString = _T( "This was some string message..." );
// Later On In Code...
CString str( cpString );
char cChar = str.GetAt( 0 );
If you write code like this, stop now and back slowly away from the keyboard – You’re Doing It Wrong!
The developer here is using a string object for a very simple operation. This is the kind of think people talk about when they say something like “using a shotgun to kill a fly”.
Extracting characters from a string (an array!) is a very basic operation – it is something we learn in our first C/C++ class or read about in our first C/C++ book. This is not something that you need a heavyweight class to help you out with.
Extracting the first character from cpString is as easy as doing one of the following:
char cChar = *cpString;
char cChar = cpString[ 0 ];
Remember – constructing and initializing an object always takes longer (i.e. has more overhead) than not constructing and initializing one. Think about wether or not you really need an object before you create one. If you can get along without it, see if doing so improves things.
For reasons mentioned in a previous post, in this case, the code is better without the CString.