如何复制 CString 功能

发布于 2024-12-03 12:21:10 字数 1071 浏览 1 评论 0原文

我有一个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);

错误是:

没有调用“std::basic_ofstream >::open(CString&)”的匹配函数

在“正常”情况下,我只会做类似的事情:

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

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

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

谢谢

I have an MFC source file that I need to compile under Qt. This file uses MFC/ATL CString. Specifically it uses a CString as an argument to iostream::open(). I have written a CString class that inherits from QString so that I can use most of QStrings' functionality.

My main concern is that I cannot get my CString implementation to work where iostream::open() is called:

Here is a bit of my class declaration:

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

And, here is a portion of code to use the CString:

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

outfile.open(Path);

The error is:

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

Under 'normal' circumstances, I would simply do something like:

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

However, that is not an option. No modification of the original code is authorized. :(

Is there a way to do this, or am I going to have to rebuild the class using the same, rather more complex and lengthy code, that Microsoft uses in cstringt.h?

Thanks

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

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

发布评论

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

评论(6

孤芳又自赏 2024-12-10 12:21:11

根据CString 有一个重载的运算符 LPCTSTR,这就是无需任何显式转换即可工作的方式。

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

According to this, CString has an overloaded operator LPCTSTR, which is how this works without any explicit conversions.

My guess is that if you want to emulate this behaviour, you'll need to provide a similar overload. LPCTSTR is somewhat retarded; so operator const char * is probably better.

烦人精 2024-12-10 12:21:11

首先,CString 的正确设计是包装 QString,而不是继承它。毕竟,您只是重新连接方法调用。

class CString {
public:
    /* ... */
private:
    QString internal;
};

编译器抱怨 ofstream::open() 方法未定义用于获取 CString 参数。这意味着 CString 没有被直接获取(毕竟,ofstream 是一个标准类)。您可以在类中编写强制转换,以便在需要时将其转换为 char *。

class CString {
public:
    /*...*/
    operator const char *()
    {
         return internal.c_str();
    }
private:
    QString internal;
};

这可能会解决问题。
希望这有帮助。

First of all, the correct design for your CString is to wrap QString, instead of inherit from it. After all, you're just rewiring the method calls.

class CString {
public:
    /* ... */
private:
    QString internal;
};

The compiler is complaining about ofstream::open() method not defined for taking a CString argument. This means that the CString was not being taken directly (after all, ofstream is an standard class). You could write a cast in the class so it is converted to char * when required.

class CString {
public:
    /*...*/
    operator const char *()
    {
         return internal.c_str();
    }
private:
    QString internal;
};

This will probably solve the problem.
Hope this helps.

你列表最软的妹 2024-12-10 12:21:11

为您的 CString 类定义一个类型转换运算符,如下所示:

operator const char*() { return this->toStdString().c_str(); }

Define a typecast operator for your CString class as follows:

operator const char*() { return this->toStdString().c_str(); }
坏尐絯℡ 2024-12-10 12:21:11

我编写了一个继承自 QString 的 CString 类,以便我可以使用 QString 的大部分功能

不要这样做。 QString 没有 virtual 析构函数,并且不打算从中继承。在这方面,QStringstd::basic_string 没有什么不同,这个其他答案

I have written a CString class that inherits from QString so that I can use most of QStrings' functionality

Do not do this. QString does not have a virtual destructor, and is not intended to be inherited from. In this respect, QString is no different than std::basic_string which is outlined in this other answer.

倾城泪 2024-12-10 12:21:11

不要尝试将 MFC 代码硬塞到 Qt 环境中。也永远不要从没有虚拟析构函数的对象继承。其中包括 QString 和大多数 STL,例如 std::string ,

std::ofstream 是标准 C++ 的一部分,并且需要 C 风格的字符串const char*

直接使用 QString 执行类似的操作:

ofstream outfile;
QString path("dump.txt");
outfile.open(path.toStdString().c_str());

或者直接使用 std::string

ofstream outfile;
std::string path("dump.txt");
outfile.open(path.c_str());

MFC 依赖于 Windows,因此在 Qt 中使用它会删除跨平台代码的优点和不否则就很有意义,因为我认为 Qt 框架几乎在所有方面都优于 MFC。

Don't attempt to shoehorn MFC code into a Qt environment. Also never inherit from objects that don't have virtual destructors. Which includes QString and most of STL such as std::string

std::ofstream is part of standard C++ and expect a c-style string const char*

Do something like this with QString directly:

ofstream outfile;
QString path("dump.txt");
outfile.open(path.toStdString().c_str());

Or use std::string directly:

ofstream outfile;
std::string path("dump.txt");
outfile.open(path.c_str());

MFC is windows dependent so using it in Qt removes the advantage of cross platform code and doesn't make much sense otherwise as the Qt framework in my opinion is superior to MFC in almost every way.

善良天后 2024-12-10 12:21:11

您应该尝试一个简单的:

typedef QByteArray CString;

// And your code should work as expected
ofstream outfile;
CString Path("dump.txt");
outfile.open(Path);

QByteArray 具有大多数QString 函数,以及operator const char *

如果您仍然想围绕 QString 编写新的包装类,多亏了该运算符,您可以使用 QString::toAscii() 而不是更长的 QString ::toStdString()::c_str()。

You should try a simple:

typedef QByteArray CString;

// And your code should work as expected
ofstream outfile;
CString Path("dump.txt");
outfile.open(Path);

QByteArray has most of QString functions, and the operator const char *.

And if you still want to write a new wrapper class around a QString, thanks to that operator you can use QString::toAscii() instead of the longer QString::toStdString()::c_str().

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