类std::wstring缺少一些针对“普通”c字符串(和字面量)的操作。
我想在自己的自定义类中添加这些缺失的操作:
#include <string>
class CustomWString : public std::wstring {
public:
CustomWString(const char*);
const char* c_str(void);
};
这段代码在Ubuntu Karmic下使用g++ v4.4.1编译正常。
但是我想知道是否有反对意见?
编辑:以下是一些例子,以澄清我所说的“缺少操作”:
std::wstring foo("hello"); // requires the L() macro or something like that
std::string bar("hello");
std::wstring goose(bar.c_str());
foo="hello"; // requires the L() macro
foo=bar.c_str();
foo=bar;
编辑:我希望这个内容能够“集中”处理。因为我有一个项目需要从微软Windows平台移植过来,其中涉及到成千上万个失败的操作。
好消息是:有一个中心位置定义了要使用的字符串类型,例如:
#ifdef WINDOWS_OS
typedef std::wstring AppStringClass;
#else
typedef std::string AppStringClass;
#endif
char*
的转换。 - James McNellisstd::wstring str = encode("str")
并不会增加太多工作量。另外,请删除你的代码片段中的void
,它在那里完全没有用处。 - avakar