如何复制 CString 功能
我有一个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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
根据此,
CString
有一个重载的运算符 LPCTSTR
,这就是无需任何显式转换即可工作的方式。我的猜测是,如果您想模拟这种行为,您需要提供类似的重载。
LPCTSTR
有点迟钝;所以operator const char *
可能更好。According to this,
CString
has an overloadedoperator 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; sooperator const char *
is probably better.首先,
CString
的正确设计是包装QString
,而不是继承它。毕竟,您只是重新连接方法调用。编译器抱怨
ofstream::open()
方法未定义用于获取CString
参数。这意味着CString
没有被直接获取(毕竟,ofstream
是一个标准类)。您可以在类中编写强制转换,以便在需要时将其转换为 char *。这可能会解决问题。
希望这有帮助。
First of all, the correct design for your
CString
is to wrapQString
, instead of inherit from it. After all, you're just rewiring the method calls.The compiler is complaining about
ofstream::open()
method not defined for taking aCString
argument. This means that theCString
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.This will probably solve the problem.
Hope this helps.
为您的
CString
类定义一个类型转换运算符,如下所示:Define a typecast operator for your
CString
class as follows:不要这样做。
QString
没有virtual
析构函数,并且不打算从中继承。在这方面,QString
与std::basic_string
没有什么不同,这个其他答案。Do not do this.
QString
does not have avirtual
destructor, and is not intended to be inherited from. In this respect,QString
is no different thanstd::basic_string
which is outlined in this other answer.不要尝试将 MFC 代码硬塞到 Qt 环境中。也永远不要从没有虚拟析构函数的对象继承。其中包括 QString 和大多数 STL,例如 std::string ,
std::ofstream 是标准 C++ 的一部分,并且需要 C 风格的字符串
const char*
直接使用
QString
执行类似的操作:或者直接使用
std::string
: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 asstd::string
std::ofstream
is part of standard C++ and expect a c-style stringconst char*
Do something like this with
QString
directly:Or use
std::string
directly: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.
您应该尝试一个简单的:
QByteArray
具有大多数QString
函数,以及operator const char *
。如果您仍然想围绕 QString 编写新的包装类,多亏了该运算符,您可以使用 QString::toAscii() 而不是更长的 QString ::toStdString()::c_str()。
You should try a simple:
QByteArray
has most ofQString
functions, and theoperator const char *
.And if you still want to write a new wrapper class around a
QString
, thanks to that operator you can useQString::toAscii()
instead of the longerQString::toStdString()::c_str()
.