为 Unicode 和 ANSI 重载 toString
Overload toString for both Unicode and ANSI
在为 class 实现 toString()
函数时,我应该同时实现 unicode 和 ascii 版本吗?
class MyClass
{
public:
string toString()
{
return "foo";
}
// Should the unicode version be implemented aswell?
wstring toString()
{
return L("foo");
}
};
// Does this suffice?
#ifndef UNICODE
typedef string tstring;
#else
typedef wstring tstring;
#endif
class MyClass
{
public:
tstring toString()
{
return "foo";
}
};
还有函数重载可以转换成int吗?
class MyClass
{
public:
// ???
int operator>>(MyClass& obj)
{
return myInt;
}
private:
int myInt;
};
很明显,这个问题是关于 Windows 编程的,尽管这并没有反映在(我写这篇文章时的当前)标签中。
对于 Windows,您应该使用 wchar_t
类型,而不必费心维护与您 (1) 的早期 Windows 版本的兼容性甚至不能为现代 Visual C++ 生成可执行文件。
就是这样。
(1) 如果您 可以 生成 Windows 9x 可执行文件,那么支持旧 Windows 的方法将不是直接使用陈旧的 T
东西或 ANSI 文本,而是使用 Microsoft 的 Layer for Unicode 从 2000 年开始,现在已经 15 年前了;这顶帽子很老了。
在为 class 实现 toString()
函数时,我应该同时实现 unicode 和 ascii 版本吗?
class MyClass
{
public:
string toString()
{
return "foo";
}
// Should the unicode version be implemented aswell?
wstring toString()
{
return L("foo");
}
};
// Does this suffice?
#ifndef UNICODE
typedef string tstring;
#else
typedef wstring tstring;
#endif
class MyClass
{
public:
tstring toString()
{
return "foo";
}
};
还有函数重载可以转换成int吗?
class MyClass
{
public:
// ???
int operator>>(MyClass& obj)
{
return myInt;
}
private:
int myInt;
};
很明显,这个问题是关于 Windows 编程的,尽管这并没有反映在(我写这篇文章时的当前)标签中。
对于 Windows,您应该使用 wchar_t
类型,而不必费心维护与您 (1) 的早期 Windows 版本的兼容性甚至不能为现代 Visual C++ 生成可执行文件。
就是这样。
(1) 如果您 可以 生成 Windows 9x 可执行文件,那么支持旧 Windows 的方法将不是直接使用陈旧的 T
东西或 ANSI 文本,而是使用 Microsoft 的 Layer for Unicode 从 2000 年开始,现在已经 15 年前了;这顶帽子很老了。