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 be used unintentionally, So it was blocked. If you understand the reason, it is easy to solve it. Just explicitly limit the scope of object a. switch (t) { case 0: {//added for fix problem int a = 0; break; }//added for fix problem default: break; }
If you really need to use the object a in the entire switch statement, move int a = 0; before the switch statement. However, judging from the original sentence, the intention does not seem to be the case, so the previous solution is recommended.
has it ended? No. Let us continue to study the exact meaning of "initialization" in the error message. C++ attaches great importance to initialization, so it often gives us an illusion that it seems that the object must go through the initialization process at the definition. What is the real situation? Let's prove it with examples.
switch (t)
{ case 0: int a; a = 0; break; default: break; } Compile, no error is reported this time. Obviously int a; defines the object, but has not been initialized, otherwise the original error should be reported. Look at user-defined types again. class B { }; switch (t) { case 0: B b; break; default: break; } There is no error in the compilation result, so the class without a constructor still has no initialization process. If you add a constructor to the class, the situation is different. class B { B(){}//added for initialization
public://added for initialization
};
This will reproduce the original error. Prove that with the constructor, the compiler will perform initialization processing and perform security checks.
From the above experiments, you can intuitively experience some basic C++ concepts and principles, and increase the depth of understanding.
1. int a = 0; is both a declaration and a definition, including initialization; whether int a; is a declaration or a definition depends on the context, but if it is a definition, initialization will not be included; a = 0; is just an assignment statement, in this sentence The former object already exists.
2. In order to avoid unnecessary overhead, by default, the compiler does not provide an initialization process when the programmer does not clearly indicate in the code. For some classes that need to be initialized, please provide a constructor. Here is a C++ design philosophy: usually you will face a variety of choices, so please control the code accurately, and the benefits are program features such as security, speed, memory overhead, etc. that you can freely choose and deploy.
3. Pay close attention to the use of labels in the program, especially conventional labels such as case and default, otherwise they may destroy the correct state of the object. If object initialization is provided, you can get additional help from the compiler.
Question:
1>
"
If the program jumps from switch to default during execution, the object a will not be initialized correctly. Ensuring the initialization of the object is an important design philosophy of C++, so the compiler will strictly check this violation In the case, like the above example code, the default statement is not used after the default statement, but considering that future code changes may be used unintentionally, it is also blocked.
"
It will also consider that future code changes may be used unintentionally.
2>
class B
{ Why does the author say that there is no initialization when declaring a B object ? Does n't the class use its default constructor to initialize the object when no constructor is declared? Why does the author say there is no initialization?
};
3>
Why do you say that there will be problems without initialization? All objects in C++ must be initialized? That's not the case.
The author seems to mean that.!

First of all, it is necessary to admit that the author raised this question very well, and many people will encounter this situation, but his own understanding of this question personally thinks that there is a misunderstanding or inadequate. Here is my analysis:
Code:
switch (t) {case 0: int a = 0; break; default: break;}
The error message under g++ is:
case_init.cpp:9: error: jump to case label
case_init.cpp:7: error: crosses initialization of `int a'.
First of all, the author mentioned here that the problem lies in the local variable or called The scope of automatic variables is very good. The scope of local variables is only between curly braces. So scheme 1:
Code:
switch (t) {case 0: {//added for fix problem int a = 0; break;}//added for fix problem default: break;}
The problem is solved. Within the new constraint, the variable a works very well, and the intention is also correctly realized. Then program 2:
Code:
switch (t) {case 0: int a; a = 0; break; default: break;}
That’s right. It seems that the two schemes have achieved their goals and are equivalent. So the author completely turned his attention to the problem of initialization, stuck on it, and at the end he didn't explain why it can be initialized sometimes and sometimes it can't.
In fact, one step further, it is completely clear. I changed the program and added a few print statements:
Code:
#include <stdio.h> int main() {printf("Please enter a number: "); int t; scanf("%d", &t); switch (t) {case 0: int a; printf("/na = %d/n", a = 0); break; case 1: printf("/na = %d/n", a = 1); break; default: printf("/na = %d/n ", a = t); break;}}
After compiling and running, everything is normal. Some people may be surprised that int a is declared in case 0! If you choose 1 when running, won't you skip over? How can it be used in case 1 and default? As everyone knows, whether you choose 0 or not, the local variable a defined in this way is valid within its scope (from the declaration to the closing curly brace). It's time to understand why the compiler reacts to int a = 0; initialization! Although you know that a is only used in case 0, a is not used in case 1 or default (not the above example), but the compiler does not understand your mind, in order to avoid using the variable in other places. Unnecessary trouble, make an error reminder. As for the following class example, the truth is the same. The so-called full letter is worse than no book, not to mention that it is not a master’s handwriting.

Comments