c++ - 如何复制 CString 功能

标签 c++ qt mfc

我有一个MFC源文件需要在Qt下编译。此文件使用 MFC/ATL CString。具体来说,它使用 CString 作为 iostream::open() 的参数。我编写了一个继承自 QString 的 CString 类,这样我就可以使用 QString 的大部分功能。

我主要担心的是我无法让我的 CString 实现在调用 iostream::open() 的地方工作:

这是我的一些类声明:

    class CString : public QString {
    public:
        CString() : QString() {}
        CString(const QString& other) : QString(other) {}
        CString(const CString& other) : QString(other) {}
        CString(_In_opt_z_ const XCHAR* pszSrc) : QString( pszSrc ) { *this = pszSrc; }
        CString(const char* pszSrc) : QString( pszSrc ) {}
        ...
    }

下面是使用 CString 的部分代码:

ofstream outfile;
CString Path("dump.txt");

outfile.open(Path);

错误是:

no matching function for call to 'std::basic_ofstream >::open(CString&)'

在“正常”情况下,我会简单地做如下事情:

outfile.open(Path.toStdString().c_str());

但是,这不是一个选项。未经授权不得修改原始代码。 :(

有没有办法做到这一点,或者我是否必须使用 Microsoft 在 cstringt.h 中使用的相同但更复杂和冗长的代码来重建类?

谢谢

最佳答案

根据 thisCString 有一个重载的operator LPCTSTR,这就是它在没有任何显式转换的情况下的工作方式。

我的猜测是,如果您想模拟这种行为,则需要提供类似的重载。 LPCTSTR 有点迟钝;所以 operator const char * 可能更好。

关于c++ - 如何复制 CString 功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7325993/

相关文章:

c++ - 使用 libfmt 打印或格式化自定义类型的简单方法是什么?

c++ - 返回值是部分的而不是完整的

qt - Qt组件边框颜色

c++ - 为用户切换功能区类别

c++ - 为 MFC 文档 View 子类化 ListView

c++ - CDialog ShowWindow问题

c++ - 是什么让 Qt 小部件及其布局正确运行(就其大小而言)?

c++ - 在 Qt Quick 2 中使用 QVTKOpenGLWidget

c++ - 在 QtCreator 3.6 中运行两个应用程序

c++ - 新 Mac App Store 上的 Qt 应用程序