Skip to main content
gamedev.net
🔒 Locked

Quick question:

Started by Floppy Dec 19, 2001 at 6:00 PM 2 replies 400+ views
Original Post
Floppy
Floppy
I''m just wondering why I need braces around this code or MSVC won''t compile it:
  
//  The main window message handler

LRESULT CALLBACK WndProc(HWND hWnd,		//  The application''s window handle

						UINT message,	//  The message to be processed

						WPARAM wParam,	//  More information about the message

						LPARAM lParam)	//  More information about the message

{
	// Get the Window context

	GLWindow* window = (GLWindow*)(GetWindowLong(hWnd, GWL_USERDATA));

	//  Handle the message

	switch(message)
	{
		//  When the window is being created

		case WM_CREATE:
			{
				// Store the Window context

				CREATESTRUCT* creation = (CREATESTRUCT*)(lParam);
				window = (GLWindow*)(creation->lpCreateParams);
				SetWindowLong(hWnd, GWL_USERDATA, (LONG)(window));
			}

			return FALSE;
			break;
..........

  
So, why are does their have to be curly braces in that WM_CREATE case? I can''t see how scope would matter in that problem. Is it how Windows defined WM_CREATE? Just a question that''s been bothering me for a while.
Null and Void
Null and Void
It requires braces since you''re creating a variable within the switch statement. It''s a scope issue. Look at the following (illegal) code:
  
switch(Fish) {
case 0:
int a = 7;
break;
case 1:
a = 5;
break;
}

See how a''s scope is somewhat weird? Should "case 1" still be allowed to access it? Would it still be set to 7? So, you''re left with the following two possibilities for scope clarity:
  
int a;
switch(Fish) {
case 0:
a = 7;
break;
case 1:
a = 5;
break;
}
// OR

switch(Fish) {
case 0:
{
int a = 7;
}
break;
case 1:
// Something

break;
}


[Resist Windows XP''s Invasive Production Activation Technology!]
scaught
scaught
Sometimes it helps to hit F1 on the error the compiler gives you:

Compiler Error C2360


initialization of 'identifier' is skipped by 'case' label

The specified identifier initialization can be skipped in a switch statement.

It is illegal to jump past a declaration with an initializer unless the declaration is enclosed in a block.

The scope of the initialized variable lasts until the end of the switch statement unless it is declared in an enclosed block within the switch statement.


Edited by - scaught on December 19, 2001 7:16:24 PM
Floppy
Floppy
Thanks Null and Void. Forgot about the scope of the other case labels. Thanks again!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.