1. error
switch(something)
{
case a:
int a = 0;
break;
default:
break;
}
The results given:
error: cannot jump from switch statement to this case label……
2. The cause of the error
Study the fundamental reason is that C ++ is a rule: in any scope, if there is a variable initialization statement, the initialization statement can not be skipped, be sure to perform !
The emphasis here Once the initialization can not be skipped in the variable scope, but you can skip the entire scope!
For example, initialization statement int a = 0, although there is scope in which they are entered, but the risk is not executed, so on the error!
3. How to Modify
① put int a; and the switch is moved between the case:
switch(something)
{
int a;
case a:
a = 0;
break;
default:
break;
}
② In the case + scope symbols {}
switch(something)
{
case a:
{
int a = 0;
break;
}
default:
break;
}
These two changes are guaranteed just go to a scope, will be executed to initialize a statement!
4. True or False
switch(something)
{
case a:
int a;
break;
case b:
a = 5;
break;
default:
break;
}
In C ++, is compiled and executed correctly, because: int a just define a, and not initialized, not violated the above rules!
Compile time, the compiler to allocate space when the case a compile time assignment to case b, A is the whole scope of the {switch}, no problem.
5. Statement on the definition and initialization:
① declare the variable does not allocate memory space;
② defined variables int a, compile time will be allocated memory, but does not generate any executable code,
So int a sentence at compile time only useful when performed by skipping the time does not matter!
③ initialize variables space allocation and initialization (space allocation, assignment runtime initialization compile time), if present, must be executed!