28

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.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
user1768079
  • 683
  • 3
  • 10
  • 18

3 Answers3

46

CString is neither a C nor a C++ type. It appears to be a Microsoft invention that is essentially an alternative to std::string:

  • CString objects can grow as a result of concatenation operations.
  • CString objects follow "value semantics." Think of a CString object as an actual string, not as a pointer to a string.
  • You can freely substitute CString objects for const char* and LPCTSTR function arguments.
  • A conversion operator gives direct access to the string's characters as a read-only array of characters (a C-style 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.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 2
    On the positive side, `std::string` does all these things, except converting to `const char *`/`LPCSTLSKJ`, for which you simply call `my_str.c_str()`. – Potatoswatter Jan 23 '13 at 06:36
  • 3
    @Potatoswatter: And [it's actually a _good_ thing that this is not implicit](http://stackoverflow.com/questions/492061/why-doesnt-stdstring-provide-implicit-conversion-to-char). No surprise, however, that Microsoft decided to break the convention on that one; that's rather their MO, innit. – Lightness Races in Orbit Jan 23 '13 at 06:37
  • As I only run Linux and don't have access to Visual C++ it's of little use to me, but I just saw it in quite a few code examples while researching string processing in c/c++ and the merits of using cstring vs string object. Thank you for the reply – user1768079 Jan 23 '13 at 07:28
  • 6
    It does not "appears to be". It **IS** an MS invention that's part of the MFC library, invented to wrap Win16 and 32 APIs even before the C++ standard was established. It exist still today since it is granted to map the OS API (unlike std::string, that use CRT for its own functions). Today there is most likely no need, but programs that are not targeted to be multiplatform take some advantages from that. – Emilio Garavaglia Jan 23 '13 at 10:22
  • 3
    @EmilioGaravaglia: "is" leads to "appears to be", by simple cause and effect. :) You'll find that my wording was chosed based upon my own lack of familiarity with MFC, rather than to imply that `CString` somehow only half-exists! – Lightness Races in Orbit Jan 23 '13 at 18:17
29

Many GUI frameworks have their own string class. e.g. QT has the QString, wxWindows has wxString. In this case MFC has the CString, (along with other convenience containers like CArray, CList, CMap, etc.). 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.

acraig5075
  • 10,588
  • 3
  • 31
  • 50
15

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 both are specializations of the std::basic_string template, but as a beginner you can safely ignore those).

Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
  • 1
    Yup, important point, it's quite confusing `CString` and "C-string". It can be *even more* confusing, because `std::string::c_str()` returns "C-string" **but** it *can* be assigned to `CString` (whereas `std::string` cannot). – starriet Jul 26 '22 at 10:09