Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is `CString`?

Tags:

c++

string

mfc

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.

like image 609
user1768079 Avatar asked Jan 23 '13 06:01

user1768079


People also ask

What is CString used for?

A CString object keeps character data in a CStringData object. CString accepts NULL-terminated C-style strings. CString tracks the string length for faster performance, but it also retains the NULL character in the stored character data to support conversion to LPCWSTR .

What is a CString in C++?

cstring is the header file required for string functions. This function Returns a pointer to the last occurrence of a character in a string.

Is CString C or C++?

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.

What is include CString?

The cstring header file contains definitions for C++ for manipulating several kinds of strings. Include the standard header into a C++ program to effectively include the standard header <string. h> within the std namespace.


2 Answers

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.

like image 129
Lightness Races in Orbit Avatar answered Sep 28 '22 09:09

Lightness Races in Orbit


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.

like image 30
acraig5075 Avatar answered Sep 28 '22 10:09

acraig5075