Q***5 发帖数: 994 | 1 I'm working through D. Rajan's Challenges with C++, and have some questions | n******t 发帖数: 4406 | 2 The definition appears at line X, makes the i available to the whole switch
control statement and you initialized it, C++ does not allow this to happen,
because other wise you could use some variable without actually
initializing it.
This type of questions is really pedantic, a well-trained programmer won't
really come to situations like this.
questions
【在 Q***5 的大作中提到】 : I'm working through D. Rajan's Challenges with C++, and have some questions
| Q***5 发帖数: 994 | 3 Thanks. But I still don't quite understand your point about initialization.
For example, if we change line X to
int i; i = 10;
to change initialization to assignment, is that allowed?
or no declaration of variable inside a switch at all?
switch
happen,
【在 n******t 的大作中提到】 : The definition appears at line X, makes the i available to the whole switch : control statement and you initialized it, C++ does not allow this to happen, : because other wise you could use some variable without actually : initializing it. : This type of questions is really pedantic, a well-trained programmer won't : really come to situations like this. : : questions
| n******t 发帖数: 4406 | 4
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This is allowed. Because now you define a variable WITHOUT initializing it.
So C++ believes that you will do the assignment yourself. Just like those
simple
case with out switch control.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
It is allowed, but a declaration of variable with initializer while could
be potentially jumped over, is not allowed.
【在 Q***5 的大作中提到】 : Thanks. But I still don't quite understand your point about initialization. : For example, if we change line X to : int i; i = 10; : to change initialization to assignment, is that allowed? : or no declaration of variable inside a switch at all? : : switch : happen,
| Q***5 发帖数: 994 | 5 Got you. Thanks.
【在 n******t 的大作中提到】 : : ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ : This is allowed. Because now you define a variable WITHOUT initializing it. : So C++ believes that you will do the assignment yourself. Just like those : simple : case with out switch control. : ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ : It is allowed, but a declaration of variable with initializer while could : be potentially jumped over, is not allowed.
|
|