C++ 什么是`CString`?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14473526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:22:03  来源:igfitidea点击:

What is `CString`?

c++stringmfc

提问by user1768079

Why do I see some code using CStringsdeclared differently.

为什么我看到一些代码 usingCStrings声明不同。

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 CStringsdeclare 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.

有区别吗?我发现的所有参考资料都CStrings像我在第一个例子中所做的那样声明它,我只看到它在论坛和类似的地方以另一种方式完成,在那里人们给出了例子。

回答by Lightness Races in Orbit

CStringis neither a C nor a C++ type. It appears to be a Microsoft inventionthat is essentially an alternative to std::string:

CString既不是 C 也不是 C++ 类型。它似乎是 Microsoft 的一项发明,本质上是以下内容的替代品std::string

  • CStringobjects can grow as a result of concatenation operations.
  • CStringobjects follow "value semantics." Think of a CStringobject as an actual string, not as a pointer to a string.
  • You can freely substitute CStringobjects for const char*and LPCTSTRfunction arguments.
  • A conversion operator gives direct access to the string's characters as a read-only array of characters (a C-style string).
  • CString对象可以因连接操作而增长。
  • CString对象遵循“值语义”。将CString对象视为实际的字符串,而不是指向字符串的指针。
  • 您可以随意替换CString的对象const char*LPCTSTR函数的参数。
  • 转换运算符可以将字符串的字符作为只读字符数组(C 样式字符串)直接访问。

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).

(a) 人们知道你在说什么;
(b) 您的代码是可移植的;
(c) 您正在编写每个人都可以根据全球公认的 ISO C++ 标准合理化的 C++,很多人为此花费了很多很多时间来争论这个明确的目的(你知道,而不是少数人一间公司办公室的房间)。

It will only be available when you are programming with Microsoft Visual C++, which is substantially limiting.

它仅在您使用Microsoft Visual C++进行编程时可用,这在很大程度上受到限制。

回答by acraig5075

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.

许多 GUI 框架都有自己的字符串类。例如 QT 有QString,wxWindows 有wxString。在这种情况下,MFC 具有 CString。在 MFC gui 代码的上下文中使用 CString 既方便又有意义,因为那时您已经严重依赖 Visual C++ 并且不会设想代码可移植性。我会小心一揽子声明说忽略它,因为它是非标准的 - 这一切都取决于上下文。

回答by Ulrich Eckhardt

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).

以防万一这是混淆的原因:“CString”中的“C”只是 MFC 中所有类都具有的前缀。MFC 是 Microsoft 封装了 win32 API 的 C++ 库。这个字符串类与“C 字符串”关系不大,它用于描述 C 语言提供的字符串处理工具。C 语言只提供字符串处理函数,这些函数对指向表示字符串的 char 数组的指针进行操作,并且它们要求最后一个字符是 NUL(也称为 NUL 终止或零终止)。请注意,C++ 本身也有一个字符串类std::string(嗯,实际上还有 std::wstring 和 std::basic_string 模板,但作为初学者,您可以放心地忽略它们)。