我有一个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 中使用的相同但更复杂和冗长的代码来重建类?
谢谢
最佳答案
根据 this ,CString
有一个重载的operator LPCTSTR
,这就是它在没有任何显式转换的情况下的工作方式。
我的猜测是,如果您想模拟这种行为,则需要提供类似的重载。 LPCTSTR
有点迟钝;所以 operator const char *
可能更好。
关于c++ - 如何复制 CString 功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7325993/