【发布时间】:2017-07-26 17:19:29
【问题描述】:
我正在通过控制台从用户处读取文件目录,我必须将值存储在pxcCHAR* 变量中,即SDK 的typedef 用于wchar_t*。
我发现我可以通过执行以下操作将std::string 转换为std::wstring。
#include <iostream>
int main(){
std::string stringPath;
std::getline(std::cin, stringPath);
std::wstring wstrPath = std::wstring(stringPath.begin(), stringPath.end());
const wchar_t* wcharPath = wstrPath.c_str();
return 0;
}
当我运行这段代码时,我通过调试看到了这些值。
stringPath= "C:/Users/"
wstrPath= L"C:/Users/"
wcharPath= 0x00b20038 L"C:/Users/"
连接到wcharPath前面的值是从哪里来的?
此外,
因为pxcCHAR* 是typedef 的wchar_t*,所以我认为只需这样做就可以了:
pxcCHAR* mFilePath = wcharPath;
但是,我收到一条消息说“const wchar_t*”不能用于初始化“pxcCHAR*”类型的实体。
我希望隐式转换能够工作,但事实并非如此。我该如何克服这个错误?
【问题讨论】:
-
如果不指定您在转换的任一侧使用的编码,就没有必要讨论字符串类型之间的“转换”。