MFC:std::string 与 CString?

发布于 2024-11-09 23:06:03 字数 601 浏览 0 评论 0原文

将 C++ 与 MFC 结合使用。由于具有 C# 背景,我通常只使用 string 来表示所有字符串。我将它们用于类成员、方法参数和方法返回值。

现在,在 C++ 中,我有 std::string、CString、char *、LPCTSTR 等。当我设计数据成员、方法参数和方法返回值时,我应该使用哪种类型?易用性很重要,CString 似乎提供了这一点,但我的本能是倾向于便携式标准,尽管可移植性在我的优先事项列表中(现在)很低。另外,我不喜欢创建字符串缓冲区并将它们传递给方法和函数的 c 语义。

我认为从直接简化编码的角度来看,CStrings 可能具有优势。但是,总的来说,“高代码质量”的方法是什么?

编辑:

我特别关心代码中的接口点(即方法参数和返回值)。例如:

Shape::SetCaption(const char *caption) {...}

Shape::SetCaption(CString caption) {...}

Shape::SetCaption(std::string caption) {...}

Shape::SetCaption(std::wstring caption) {...}

Using C++ with MFC. Coming from a C# background I typically just use string for all, well, strings. I use them for class members, method parameters, and method return values.

Now in C++ I've got std::string, CString, char *, LPCTSTR, and more. As I design my data members, method parameters, and method return values which type(s) should I be using? Ease of use is important and CString seems to offer that but my instinct is toward portable standards although portability is pretty low on my list of priorities (now). Also, I don't like the c semantics of creating string buffers and passing them into methods and functions.

I think from an immediate ease of coding perspective CStrings probably have the edge. But, overall, what is the "high code quality" way to do this?

EDIT:

I'm especially concerned about the interface points in my code (i.e. method parameters and return values). E.g.:

Shape::SetCaption(const char *caption) {...}

Shape::SetCaption(CString caption) {...}

Shape::SetCaption(std::string caption) {...}

Shape::SetCaption(std::wstring caption) {...}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

桜花祭 2024-11-16 23:06:03

我通常更喜欢使我的编码风格适应我正在使用的框架,以与其保持一致。因此,当我使用 MFC(我已经很长时间没有使用)时,我更喜欢使用 CString(和 LPCTSTR 作为公共接口方法中的函数参数)。使用 Qt 时,我更喜欢 QString 和 Qt 容器而不是 STL 容器,对于与此类框架不直接相关的所有内容,我使用 std::string ,因为它是标准的 C++ 方式处理字符串。

它并没有产生如此巨大的差异,因为它们都提供或多或少相同的功能(并且很容易相互转换),并且当为某个框架编写代码时,它无论如何都取决于它,因此可移植性并不是那么重要巨大的担忧。

只是不要为普通的字符数组而烦恼!顺便说一句,尝试通过 const 引用 (const std::string &caption) 传递对象,而不是通过值传递对象,因为在 C++ 中变量不会自动引用,并且复制字符串可能会变得相当昂贵。

I usually prefer to adapt my coding style to the framework I'm working in to be consistent with it. So when I work with MFC (which i haven't for a long time), I prefer the use of CString (and LPCTSTR as function arguments in public interface methods). When working with Qt I prefer QString and Qt's containers over STL containers and for everything not directly related to such a framework I use std::string as it's the standard C++ way of handling strings.

It doesn't make such a huge difference, since they all offer more or less equal functionality (and are easily convertible into each other) and when code is written for a certain framework, it depends on it anyway, so portability is not such a huge concern.

Just don't bother with plain char arrays! And by the way, try to pass objects by const reference (const std::string &caption) and not by value, as in C++ variables are not automatically references and copying a string can get quite expensive.

治碍 2024-11-16 23:06:03

MFC 是在您使用 CString 的预期下编写的。当函数使用参数返回字符串时,这一点尤其明显。例如,比较这两个对 GetWindowText 的调用:

CString s1;
wnd.GetWindowText(s1);

std::wstring s2(SOME_MAX, 0);
int len = wnd.GetWindowText(&s2[0], s2.size());
s2.resize(len);

不过,两者之间的转换还不错,因此您可能会妥协,对大多数事情使用 std::wstring ,并在必要时使用临时 CString 。

CString s3 = s2.c_str();
std::wstring s4 = s1;

编辑:可能有一种方法可以自动化临时 CString。公平警告,这完全是一个黑客行为。我还没有尝试过这个,所以不能保证 - 您可能会收到有关将临时引用绑定到非常量引用的警告,但您可以将其关闭。

class PopString : public CString
{
public:
    PopString(std::wstring & final) : m_final(final)
    {
    }

    ~PopString()
    {
        m_final = (PCTSTR) *this;
    }
private:
    PopString(const PopString &) {}  // private copy constructor to prevent copying
    PopString & operator=(const PopString &) {}  // private copy operator

    std::wstring & m_final;
};

std::wstring s5;
wnd.GetWindowText(PopString(s5));

MFC was written with the expectation that you'd use CString. This is especially apparent when a function uses a parameter to return a string. For example, compare these two calls to GetWindowText:

CString s1;
wnd.GetWindowText(s1);

std::wstring s2(SOME_MAX, 0);
int len = wnd.GetWindowText(&s2[0], s2.size());
s2.resize(len);

Converting between the two isn't bad though, so you might compromise by using std::wstring for most things and a temporary CString when necessary.

CString s3 = s2.c_str();
std::wstring s4 = s1;

Edit: There may be a way to automate the temporary CString. Fair warning, this is a complete hack. I haven't tried this, so no guarantees - you'll probably get warnings about binding a temporary to a non-const reference, but you can turn those off.

class PopString : public CString
{
public:
    PopString(std::wstring & final) : m_final(final)
    {
    }

    ~PopString()
    {
        m_final = (PCTSTR) *this;
    }
private:
    PopString(const PopString &) {}  // private copy constructor to prevent copying
    PopString & operator=(const PopString &) {}  // private copy operator

    std::wstring & m_final;
};

std::wstring s5;
wnd.GetWindowText(PopString(s5));
忘东忘西忘不掉你 2024-11-16 23:06:03

如果您关心可移植性并且使用 C++,请使用 std::string。如果不需要的话,没有必要使用 char 数组进行低级操作。如果您不关心可移植性,并且平台提供的字符串提供了您需要的更多功能,请务必使用它们。它们实际上可能针对平台进行了更优化。

If you care for portability and you're using C++ use std::string. No point going low-level with char arrays if you don't need to. If you don't care for portability and the platform-provided strings provide more of the features you need, by all means, use them. They might actually be more optimized for the platform.

笑梦风尘 2024-11-16 23:06:03

不要使用 CString。它使用 COW 实现,该实现非常容易受到线程等问题的影响。请勿使用 char*LPCTSTR(这只是不同名称下的 const char*const wchar_t* ),因为它们不管理自己的内存。在 Windows 上使用 std::string 表示 8 位代码点,或使用 std::wstring 表示 16 位代码点(Unix 上为 32 位)。

Do not use CString. It uses a COW implementation which is very vulnerable to things like threading. Do not use char* or LPCTSTR (which is just const char* or const wchar_t* under a different name), as they do not manage their own memory. Use a std::string for 8-bit codepoints or std::wstring for 16-bit codepoints on Windows (32bit for Unix).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文