 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Jonathan Clements Guest
|
Posted: Sun Jun 29, 2003 6:26 pm Post subject: C++ Scope |
|
|
Hi all,
Does 'for' have it's own scope? In one compiler I'm quite happy
writing:-
for(unsigned long i=1; i <= WHATEVER; i++) { /* something here */ } and
then (not nested)
for(unsigned long i=1; i <= WHATEVER2; i++) { /* something here2 */ }
Probably bad practice, but as far as I understand C++, 'i' should be a
temp within the scope of the for loop. Some compilers allow this, others
don't.... I'm wondering if I'm mis-understanding the scope rules or the
compilers are.
Thanks,
Jon.
|
|
| Back to top |
|
 |
Kurt Krueckeberg Guest
|
Posted: Sun Jun 29, 2003 6:30 pm Post subject: Re: C++ Scope |
|
|
| Quote: | Hi all,
Does 'for' have it's own scope? In one compiler I'm quite happy
writing:-
for(unsigned long i=1; i <= WHATEVER; i++) { /* something here */ }
and
then (not nested)
for(unsigned long i=1; i <= WHATEVER2; i++) { /* something here2 */ }
Probably bad practice, but as far as I understand C++, 'i' should be a
temp within the scope of the for loop. Some compilers allow this, others
don't.... I'm wondering if I'm mis-understanding the scope rules or the
compilers are.
Your code is correct, even if some compilers incorrectly complain. |
|
|
| Back to top |
|
 |
Stuart Golodetz Guest
|
Posted: Sun Jun 29, 2003 8:08 pm Post subject: Re: C++ Scope |
|
|
"Jonathan Clements" <jonathan (AT) psilocybe (DOT) demon.co.uk> wrote
| Quote: | Hi all,
Does 'for' have it's own scope? In one compiler I'm quite happy
writing:-
for(unsigned long i=1; i <= WHATEVER; i++) { /* something here */ }
and
then (not nested)
for(unsigned long i=1; i <= WHATEVER2; i++) { /* something here2 */ }
Probably bad practice, but as far as I understand C++, 'i' should be a
temp within the scope of the for loop. Some compilers allow this, others
don't.... I'm wondering if I'm mis-understanding the scope rules or the
compilers are.
|
The compilers are. It's perfectly good (and common) practice to reuse loop
variables like i. If one of your compilers doesn't like it, just put:
#define for if(0); else for
at the top of each translation unit and it'll fix it.
HTH,
Stuart.
|
|
| Back to top |
|
 |
Jonathan Clements Guest
|
Posted: Sun Jun 29, 2003 8:30 pm Post subject: Re: C++ Scope |
|
|
Thank you to Kurt K., John H., and Stuart G. for your replies...
"Jonathan Clements" <jonathan (AT) psilocybe (DOT) demon.co.uk> wrote
| Quote: | Hi all,
Does 'for' have it's own scope? In one compiler I'm quite happy
writing:-
for(unsigned long i=1; i <= WHATEVER; i++) { /* something here */ }
and
then (not nested)
for(unsigned long i=1; i <= WHATEVER2; i++) { /* something here2 */ }
Probably bad practice, but as far as I understand C++, 'i' should be a
temp within the scope of the for loop. Some compilers allow this, others
don't.... I'm wondering if I'm mis-understanding the scope rules or the
compilers are.
Thanks,
Jon.
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|