c++stringswitch-statementwstringlpcwstr

Defining wstring after calling / concatenating LPCWSTR


I'm tried defining a wstring within a case statement. And trying to access the variable out of the case statement return out of scope.

So I'm now trying to declare a wstring outside of the switch() and define it within the switch(). But I don't know how to separate those events.

    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;
        }

Ultimately I'm trying to concatenate (...and listen...) three LPCWSTR's and I'm using std::wstring to do that. I'm open to using other means of accomplishing this task.


Solution

  • Jumping to a case isn't allowed to skip constructor/destructor calls. Creating the wstring outside of and before the switch should be fine. You do that by writing:

    std::wstring wstr;
    

    then the switch, and in each relevant case, assigning to the wstr. Alternatively, under each case open a new block scope in which it is legal to construct a new class. In code, that would look like:

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