在不使用构造函数的情况下将WCHAR*转换为wstring

ᴘᴀɴ*_*ᴛɪs 5 c++

我有一个wchar数组,我需要使用winapi函数写入数据,我想得到它std::wstring,我知道std::wstring t(wText);工作,但我不能在if语句中声明它,因为它不会存在于该范围之外.另外我不能以这种方式调用构造函数:

    WCHAR wText[256];

    std::wstring t = L"";
    if (somecondition)
    {
        t = t(wText); 
    }
Run Code Online (Sandbox Code Playgroud)

我能做什么?我希望wstring等于 ""条件不满足时和满足条件时我希望得到一个WCHAR数组.

Pet*_*erT 7

您可以std::wstring通过调用表达式中的构造函数来构造临时对象,如下所示:

t = std::wstring(wText);
Run Code Online (Sandbox Code Playgroud)

  • 呵呵.为什么不`std :: wstring t; if(somecondition)t.assign(wText);` (4认同)