CString in addition to an array of characters (or wide characters) contains the size of the string, the size of the allocated buffer, and the reference count (serving additionally as a lock flag). A buffer containing an array of characters can be significantly larger than the string contained in it, which reduces the number of calls associated with time consumption. Also, when a CString is zero size, it still contains two wchar characters.
Naturally, when you compare the size of a CString with the size of the corresponding C-style array, the array will be smaller. However, if you want to manipulate your string as broadly as CString allows you to end up defining your own variables for string size, buffer size, and sometimes the refcounter and / or guard flags. Indeed, you need to keep your string size to avoid calling strlen every time you need it. You need to separately store the size of your buffer if you allow your buffer to be larger than the length of the string, and avoid calling reallocs every time you add or subtract from the string. And so on - you trade a small increase in size for a significant increase in speed, security and functionality.
So the answer depends on what you are going to do with the string. Suppose you want a string to keep the name of your class for logging - there the C-style string (const and static) will work fine. If you need a string to manage and use it with MFC or ATL-related classes, use the CString family types. If you need to manipulate a string in the βengineβ part of your application that is isolated from its interface and can be converted to other platforms, use std :: string or write your own string type to suit your specific needs (this can really be useful when writing glue code to be placed between the interface and the engine, otherwise std :: string is preferable).
PS C ++ string comparison with char []
A source
source share