Posts

error jump to case label Code Answer's

  A piece of code from the actual project, the simplified form is as follows:   switch (t) { case 0: int a = 0; break; default: break; } Is there any problem? It seems not. Please compile it with a compiler... Huh? ! An error "error C2361: initialization of'a' is skipped by'default' label". how can that be? After a few thoughts, I realized the explanation: C++ conventions, in the block statement, the scope of the object starts from the declaration statement of the object to the end of the block statement, which means that the statement after the default label can use the object a. If the program jumps from switch to default during execution, it will cause object a to not be initialized correctly. Ensuring the initialization of objects is an important design philosophy of C++ , so the compiler will strictly check for such violations. For example, in the above example code, a is not used after the default statement, but considering that future code changes may b...