Hi, Daniel Jones!
On 13 Aug 97 07:06:00 you wrote to Aare Tali
AT> for (i = 0; i < 5; i++)
AT> do_something(i);
AT> for (int j = 0; j < 5; j++)
AT> do_something_else(j);
AT> if (i < 5)
AT> not_everything_was_done_with_i();
AT> if (j < 5)
DJ> Did you test this? If so, with what compiler?
Probably yes, and using an erlyer compiler
DJ> With BC++ 5.01, I get an error:
DJ> Undefined symbol 'j'
DJ> Why? Because the for() loop defines a block. j is local to that
DJ> block, and is out of scope in your "if(j < 5)" statement. As I recall,
DJ> this was a matter of some debate, and wasn't implemented in early
DJ> versions of C++. I'm reasonably certain that BC++ 3.1 allowed the
DJ> above code.
Yes, the scoping rule of variables defined in for() changed in the latest C++
draft, so the above code will get an error in the compilers based on the
draft.
Previously C++ handled the for (int i= ...) like C: it broke the for
statement to
A;
while(b)
{
... the block
C;
}
This way wariables defined at the first part went to the csope of the outer
block. The change made them to belong in the scope of the block of for.
Paul
... WORN disk: Write Once Read Never disk
--- OS/2 Warp
---------------
* Origin: The FlintStones' Cave in BedRock (2:371/20)
|