tag:blogger.com,1999:blog-272316722024-03-06T19:58:20.154-08:00Visual Studio's CString FAQs...Siddhartha Raohttp://www.blogger.com/profile/02278521553171894987noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-27231672.post-1163968455772984982006-11-19T11:30:00.000-08:002007-07-28T13:17:24.496-07:00What's the deal with CString and explicit constructors (C2440)?Let me first address the question:Q. What are explicit constructors?A. Constructors that use the explicit keyword. Explicit constructors are used to avoid unintentional conversions. Constructors that don't use the C++ explicit keyword are implicit by default.Q. Why are they used by CString?A. CString is functionally a string class designed to make working with TCHAR strings (TCHAR* a.k.a LPTSTR) Siddhartha Raohttp://www.blogger.com/profile/02278521553171894987noreply@blogger.comtag:blogger.com,1999:blog-27231672.post-1156185720265675002006-08-21T11:36:00.000-07:002007-07-28T13:17:52.044-07:00How to convert from std::string to CString?This is simple... Use the CString constructor.std::string strStdString ("Hello!");// Using CString ConstructorCString strCString (strStdString.c_str ());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 Siddhartha Raohttp://www.blogger.com/profile/02278521553171894987noreply@blogger.comtag:blogger.com,1999:blog-27231672.post-1156183380846224242006-08-21T10:29:00.000-07:002007-07-28T13:18:09.664-07:00How to convert from CString to std::string?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));Note that as discussed in this post, CStringA is a template specialization of class CStringT for type char avaÃlable with Visual Studio 7.x and better.Siddhartha Raohttp://www.blogger.com/profile/02278521553171894987noreply@blogger.comtag:blogger.com,1999:blog-27231672.post-1146260712960155832006-04-28T14:23:00.000-07:002007-07-28T13:18:30.649-07:00Finding correlations between the CString Family and the Standard String FamilyIf we were to draw a correlation between the CString Family and Standard String Classes, we would notice -CStringT as the equivalent of std::basic_stringCStringA as the equivalent of std::stringCStringW as the equivalent of std::wstringHowever, 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 Siddhartha Raohttp://www.blogger.com/profile/02278521553171894987noreply@blogger.comtag:blogger.com,1999:blog-27231672.post-1146259016521054662006-04-28T13:47:00.000-07:002007-07-28T13:18:50.190-07:00The 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 Siddhartha Raohttp://www.blogger.com/profile/02278521553171894987noreply@blogger.comtag:blogger.com,1999:blog-27231672.post-1146256298376256612006-04-28T13:30:00.000-07:002006-04-28T14:59:01.203-07:00The 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 -CString counts amongst the most popularly used string classes - ever.People keep asking questions that have been answered many times over, but answers to which are not easily locatable.(1) and (2) are going to continue being this way for some time now.So, me thinks - Siddhartha Raohttp://www.blogger.com/profile/02278521553171894987noreply@blogger.com