【问题标题】:Defining wstring after calling / concatenating LPCWSTR在调用/连接 LPCWSTR 后定义 wstring
【发布时间】:2015-08-14 18:10:48
【问题描述】:

我尝试在 case 语句中定义 wstring。并尝试访问 case 语句之外的变量返回范围之外。

所以我现在尝试在 switch() 之外声明一个 wstring,并在 switch() 中定义它。 但我不知道如何区分这些事件。

    wstring w1;

    switch (suit)
        {
        case 0:
            std::w1(stringOne);
            break;
        case 1:
            std::w1(stringTwo);
            break;
        case 2:
            std::w1(stringThr);
            break;
        case 3:
            std::w1(stringFou);
            break;
        }

最终我试图连接(...并听...)三个 LPCWSTR,我正在使用 std::wstring 来做到这一点。我愿意使用其他方式来完成这项任务。

【问题讨论】:

  • 在 case 语句中使用 = 将字符串分配给 w1

标签: c++ string switch-statement wstring lpcwstr


【解决方案1】:

跳转到一个案例不允许跳过构造函数/析构函数调用。在开关之外和之前创建 wstring 应该没问题。你可以这样写:

std::wstring wstr;

然后是开关,并在每种相关情况下,分配给 wstr。或者,在每种情况下打开一个新的块范围,在其中构造一个新类是合法的。在代码中,它看起来像:

case 0:
{
   std::wstring w;
   // do whatever you want to do with string w...
   // ..
   // then let it 'hit' it's destructor 
}
break;

【讨论】:

    猜你喜欢
    • 2013-03-22
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    • 2012-06-06
    • 2013-01-05
    • 1970-01-01
    • 1970-01-01
    • 2016-03-10
    相关资源
    最近更新 更多