哪种类型的字符串最适合 Win32 和 DirectX?
我正在使用 DirectX 10 和 C++ 开发一个小游戏,我发现各种不同的 directx / win32 函数调用所需的各种不同类型的字符串非常糟糕。
任何人都可以推荐可用的各种字符串中哪一个是最好使用的,理想情况下,它是一种可以为其他类型(主要是 LPCWSTR 和 LPCSTR)提供良好转换的字符串类型。到目前为止,我一直在使用 std::string 和 std::wstring 并执行 .c_str() 函数以将其转换为正确的格式。
我只想获取一种类型的字符串,用于传递给所有函数,然后在函数内进行转换。
I am in the process of developing a small game in DirectX 10 and C++ and I'm finding it hell with the various different types of strings that are required for the various different directx / win32 function calls.
Can anyone recommend which of the various strings are available are the best to use, ideally it would be one type of string that gives a good cast to the other types (LPCWSTR and LPCSTR mostly). Thus far I have been using std::string and std::wstring and doing the .c_str() function to get it in to the correct format.
I would like to get just 1 type of string that I use to pass in to all functions and then doing the cast inside the function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如您所做的那样,将
std::wstring
与c_str()
一起使用。我认为没有理由在 Windows 上使用std::string
,您的代码最好始终使用本机 UTF-16 编码。Use
std::wstring
withc_str()
exactly as you have been doing. I see no reason to usestd::string
on Windows, your code may as well always use the native UTF-16 encoding.我也会坚持使用 std::wstring 。如果您确实需要在某个地方传递
std::string
,您仍然可以即时转换它:反之亦然。
I would stick to
std::wstring
as well. If you really need to passstd::string
somewhere, you can still convert it on the fly:It works the other way around as well.
如果您使用的是 Native COM(
#import
的内容),则使用_bstr_t
。它本身可以类型转换为 LPCWSTR 和 LPCSTR,并且与 COM 的内存分配模型很好地配合。不需要.c_str()
调用。If you're using Native COM (the stuff of
#import <type_library>
), then_bstr_t
. It natively typecasts to both LPCWSTR and LPCSTR, and it meshes nicely with COM's memory allocation model. No need for.c_str()
calls.