I'm using Visual Studio C++ 7 compiler and IDE, and realized something strange. Well, I've known about it and known it was strange for a while, but I'm just now curious enough to ask what the deal is.
With my IDE/compiler, with all warnings on, the programmer can do this..
for(int x=0; x<max; x++)
Objects[x].Update();
for(int x=0; x<max; x++) // new int x
Items[x].Update();
for(int x=0; x<max; x++) // new int x
Maps[x].SkewTheMoon();
I guess this would imply that the variable is being declared in a special loop-scope. But then, the programmer can also do all of this in one go..
for(int x=0; x<max; x++)
if( Objects[x].Dead )
break;
if( x < max ) // using x from loop
Kill( Objects[x] );
for(int x=0; x<max; x++) // A new int x
Maps[x].SkewTheMoon();
for(x=0; x<max; x++) // Now lacking new dec
Items[x].Update();
So what's the deal with this? I always try to avoid these strange situations, but I'm a little curious to hear how others percieve it. I'm also curious to understand what would happen behind the scenes. Does the previous x just vaporize when you declare a new one? Does the compiler create two (with different internal names)? Or does it just recycle the old one?