   switch statement, C++
  I have not understood this one. Sometimes this throws an error.switch(thing){   case zing:   TMyClass &dring=somearray[6];   //somearray[6] is a TMyClass   some more code   break;which causes my compiler to sayerror C2360: initialization of 'dring' is skipped by 'case' labelIt depends because sometimes, I have similar code in some other C++ file and it doesn't complain. Any tips? Is it valid code?I can't declare a "TMyClass &dring" somewhere at the top of my code?   
  I can't see anything obvious with that small block of code.  Can you show the actual switch statement that's causing the error?   ;switch(thing){   case zing:   { TMyClass &dring=somearray[6];   //somearray[6] is a TMyClass some more code   }   break;;  MSDN is most helpful in these situations [smile]Quote:MSDNThe initialization of identifier can be skipped in a switch statement. You cannot jump past a declaration with an initializer unless the declaration is enclosed in a block. (Unless it is declared within a block, the variable is within scope until the end of the switch statement.)It makes complete sense, considering the fall-through behavior of cases.   ;  If you have other case labels, what is the variable supposed to contain? Just use an extra block and you're fine.switch(thing){    case zing:    {   TMyClass &dring=somearray[6];   some more code   break;    };  On its own it's valid. The compiler does have problems if there is another case following.See, in C/C++ you can let one case fall through to the next (if you don't issue a return or break statement). Now if the switch jumps to the case after the variable case the variable is still in scope, but the code initialising it not.The usual workaround is to put { } - brackets inside the case:case zing:  {    TMyClass &dring=somearray[6];   //somearray[6] is a TMyClass    some more code  }  break;Now the variable has a clear scope only inside the one case.   ;  LOL epic ninja'd :P   ;  Thanks for the replies. The actual code is more like  THIS[X].THAT[Y].somearray[Z] so having a reference makes the code look cleaner.   ;Quote:Original post by V-manThanks for the replies. The actual code is more like  THIS[X].THAT[Y].somearray[Z] so having a reference makes the code look cleaner.switch(thing){   case zing: doTheFoo(somearray[6]); break;}void doTheFoo(TMyClass & dring) {   some more code};  Im pretty sure with msvc the original code will give an error (i.e. not compile), perhaps not with one case statement though?Personally I always thought this was a design flaw in the language,   