TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: DANIEL JONES
from: BALOG PAL
date: 1997-08-17 07:16:00
subject: Sunir ventures into C++

                              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)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.