Why do I see some code using CStrings
declared differently.
Some use this format
char a_c_string [];
While others use
CString another_c_string;
Is there a difference? All the references I have found on CStrings
declare it as I did in the first example, I have only seen it done the other way on forums and the like where people are giving examples.
Many GUI frameworks have their own string class. e.g. QT has the QString, wxWindows has wxString. In this case MFC has the CString. It's then convenient and makes sense to use CString when in the context of MFC gui code because then you're already heavily dependent on Visual C++ and code portability will not be envisaged. I'd be careful of blanket statements saying to ignore it because it's non-standard - it all depends on the context.
CString
is neither a C nor a C++ type. It appears to be a Microsoft invention that is essentially an alternative tostd::string
:I recommend ignoring it, so that:
(a) people know what you are talking about;
(b) your code is portable;
(c) you are writing C++ that everybody can rationalise about according to the worldwide-accepted ISO C++ standard that many, many people spend many, many hours arguing about for this express purpose (y'know, as opposed to a few guys in a room in one company's office).
It will only be available when you are programming with Microsoft Visual C++, which is substantially limiting.
CString
is used in Visual C++.Visual C++ is Microsoft's implementation of C++
Just in case that's the cause of confusion: The "C" in "CString" is just a prefix that all classes from the MFC have. The MFC is a C++ library by Microsoft wrapping the win32 API. This string class has little to do with "C strings", which is used to describe the string-handling facilities that the C language provides. The C language only provides functions for string handling that operate on a pointer to the char array representing the string and they require that the last character is a NUL (aka NUL-terminated or zero-terminated). Note that C++ itself also has a string class
std::string
(well, actually there's also std::wstring and the std::basic_string template, but as a beginner you can safely ignore those).