Why i can't do this?

Why i can't do this?
1
2
3
4
5
6
switch(x)
{
case y:
int i=anyfunction();
break;
} 

so why it's not possible declare variable in a switch?
Good question. Actually your code (as in the following) compiles and runs for me in VC++.

However, if you tried to use "i" in another part of the case statement, I think you place the compiler in a confusing situtation, as in this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

int anyfunction() { cout << "return 0" << endl; return 0;}

int main()
{
	int x = 3;
	const int y = 3;
	const int z = 4;

	switch(x)
	{
		case y:
			int i=anyfunction(); //no brackets, so i must be available for case z
//		case z:
//			i=anyfunction(); //case z -> skips definition of "i"
//			int i=anyfunction(); //case y -> redefinition of "i"
		break;
	}
	return 0;
}

Because i is defined in case 'y' and there are no scope brackets, it must be available to the end of the switch statement. But this creates a paradox. In the case of 'z' if you skip case 'y' and try to use i, you use "i" before defining it. If you define "i" in case 'z', then case 'y' defines "i", falls through to 'z' where "i" is redefined. So the compiler is does not like that. If you place brackets around the statement(s) for case 'y' then there is no more confusion for the compiler.

1
2
3
 
		case y:
			{ int i=anyfunction();}  //i only defined for case y  


I am still learning C++ myself, so you will get some more feedback on this I'm sure.
That's a lot of text to say that cases below could possibly end up not pushing the int onto the stack.
This is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
BOOL CALLBACK infdlgProc(HWND hwnd, UINT Message, WPARAM  wParam, LPARAM lParam)
{
switch(Message){
case WM_INITDIALOG:
return true;
case WM_COMMAND:
switch(LOWORD(wParam))
{ case IDOK:
int ret=wtodsk(); // error: "jump to case label" & "crosses initialization of ret" 
break;
case IDCANCEL:
EndDialog(hwnd,IDD_COLLECT_INFO);
break;
}break;
default: return false;
}
return true;
}

but if i change int ret=wtodsk();
to
1
2
int ret;
ret=wtodsk();
then everything is fine. It's really strange...
This is a little silly for basic types like int (as in your example), but is a very critical bug/problem for objects. Since compilers tend to treat basic types like objects, you get the same error messages.

Here's an example of why this is bad with objects:

1
2
3
4
5
6
7
8
9
switch(foo)
{
case 0:
  MyClass a(5);  // ctor called here
  break;

case 1:
  break;
}                // dtor called here 


Notice that if the case 1 label is taken, the code will jump over the ctor, but will still call the dtor. Calling a dtor on an object that didn't have a ctor called is potentially disasterous, hence why you'd get an error in a situation like this.

A common solution is to localize the scope of the object:

1
2
3
4
5
6
7
8
9
10
11
switch(foo)
{
case 0:
  {              // brace to localize scope
  MyClass a(5);  // ctor called here
  }              // dtor called here
  break;

case 1:
  break;
}                // dtor no longer called here 



But yeah -- this doesn't really apply to ints. So yeah it's a little strange. But in a way it makes sense.
Now i understan, thanks!
Topic archived. No new replies allowed.