Q: How to convert from std::string to CString?
A: This is simple... Use the CString constructor.
Let me add that CStringT can construct from both character or wide-character strings. i.e. It can convert from char* (i.e. LPSTR) or from wchar_t* (LPWSTR).
In other words, char-specialization (of CStringT) i.e. CStringA, wchar_t-specilization CStringW, and TCHAR-specialization CString can be constructed from either char or wide-character, null terminated (null-termination is very important here) string sources.
Q: How to convert from CString to std::string?
A: There are really many ways to do it.
But, the simplest one is just this -
CString strSomeCstring ("This is a CString Object");
// Use ANSI variant CStringA to convert to char*; construct from it -
std::string strStdString (CStringA (strSomeCstring));
CStringA is a template specialization of class CStringT for type char avaílable with Visual Studio 7.x and better.
Q: What are correlations between the CString Family and the Standard String Family?
A: If we were to draw a correlation between the CString Family and Standard String Classes, we would notice -
However, the standard library itself doesn't contain a "CString" equivalent - one that can at best be -
This makes CString (VS 7.x +) "cool".
Q: The history of CString...
A: 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 -
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 -
Note: Most of the material in this article is taken from codeguru.com
Comments
people
people
people