Problem Understanding Movement Example.

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Mastiff
Posts: 17
Joined: Thu Apr 19, 2007 9:55 pm

Problem Understanding Movement Example.

Post by Mastiff »

Hello, I've been working with C++ for about 3 months now and am baffled by one thing I found inside the Movement Example which I can't find an answer for with endless searching.

I was wondering why the encasing brackets "{ }" where needed inside of the cases for this switch event, if I remove them I get these errors from my compiler:
error: Jump to case label
error: Crosses initialization of "irr::core::vector3df v"

Code: Select all

class MyEventReceiver : public IEventReceiver
{
public:
	virtual bool OnEvent(SEvent event)
	{
if (node != 0 && event.EventType == irr::EET_KEY_INPUT_EVENT &&
				!event.KeyInput.PressedDown)
	{
			switch(event.KeyInput.Key)
			{
			case KEY_KEY_W:
			case KEY_KEY_S:
				{
					core::vector3df v = node->getPosition();
					v.Y += event.KeyInput.Key == KEY_KEY_W ? 2.0f : -2.0f;
					node->setPosition(v);
				}
				return true;
			}
	}

	return false;
	}
};
If anyone could explain to me why this is, I would greatly appreciate it.
Dances
Posts: 454
Joined: Sat Jul 02, 2005 1:45 am
Location: Canada
Contact:

Post by Dances »

you are crossing the initialization of 'v' without them

without the brackets when 's' is pressed the initialization happens, BUT if it isn't pressed the program still tries to use 'v'. If 's' weren't pressed there would be no v to manipulate; you would be trying to use a variable that doesn't exist.

You'll get the same if you have an if statement than creates a variable and then the next line uses the variable when you don't have { } encasing both.

lets see how I can explain this better

Code: Select all

if (x == 1)
   int y = 0;
   y++;
This will produce the same error.... you see... without the { } ONLY the first line after an if/case statement is executed

Code: Select all

if (x == 1)
{
   int y = 0;
   y++;
}
This won't produce the error because now y++ only happens IF x == 1

Code: Select all

if (x == 1)
   int y = 0;
   y++;
and

Code: Select all

if (x == 1)
{
   int y = 0;
}
y++;
are equivalent code.

Cases in a switch work in the same manner as if; with one exception. If one case statement is true and you don't break at the end of it then all of the case statements thereafter are executed.
Last edited by Dances on Tue May 08, 2007 2:55 am, edited 3 times in total.
cuatro
Posts: 11
Joined: Tue May 08, 2007 2:29 am

Post by cuatro »

what compiler are you using?

some compilers allow you to have switch statements without the brackets others may not. i pretty much just use visual studio 2005 and earlier and have never run into that error.

what happens when you use brackets like that is it makes the scope of the variable (in this case the vector3df v) local to that particular block of code (everything in the brackets). a bit longwinded explanation is here http://en.wikipedia.org/wiki/Variable#Scope_and_extent.

ahh looks like your using gcc cause i just googled your error and came across this post that explains it much better than i could :lol: http://www.karakas-online.de/forum/viewtopic.php?t=4194
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

You MUST use the brackets when defining a variable. The scopes inside switch statements are really weird. Mainly because switch statements are simply goto labels with a common dispatcher statement. If you leave out the brackets all jumps will require lots of initialization stuff which is not required, you might notice completely uninitialized code due to compiler-internal goto handling etc. Simply don't do it. Your code will be far more maintainable and maybe even better optimizable.
Mastiff
Posts: 17
Joined: Thu Apr 19, 2007 9:55 pm

Post by Mastiff »

Thank you all for the help, I understand now.

I can also see how this would sometimes be more useful than declaring the variables outside of the switch statement, as the current way discards the variables after they leave the scope.

Your also right I am using Code::Blocks with the gcc compiler.

Again thank you.
Post Reply