Consider following code:
#include <iostream>
using namespace std;
int main()
{
int x, y, i;
cin >> x >> y >> i;
switch(i) {
case 1:
// int r = x + y; -- OK
int r = 1; // Failed to Compile
cout << r;
break;
case 2:
r = x - y;
cout << r;
break;
};
}
G++ complains crosses initialization of 'int r'
.My questions are:
- What is
crosses initialization
? - Why do the first initializer
x + y
pass the compilation,but the later failed? - What are the problems of so-called
crosses initialization
?
EDIT:
I know I should use brackets to specify the scope of r
but I want to know why,for example why non-POD could not be defined in multi-case switch statement.
Thanks.