Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   C++ Scope (http://www.velocityreviews.com/forums/t267862-c-scope.html)

Jonathan Clements 06-29-2003 06:26 PM

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.





Kurt Krueckeberg 06-29-2003 06:30 PM

Re: 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.
>

Your code is correct, even if some compilers incorrectly complain.



Stuart Golodetz 06-29-2003 08:08 PM

Re: C++ Scope
 
"Jonathan Clements" <jonathan@psilocybe.demon.co.uk> wrote in message
news:bdnanh$lv7$1$8302bc10@news.demon.co.uk...
> 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.

> Thanks,
>
> Jon.




Jonathan Clements 06-29-2003 08:30 PM

Re: C++ Scope
 
Thank you to Kurt K., John H., and Stuart G. for your replies...

"Jonathan Clements" <jonathan@psilocybe.demon.co.uk> wrote in message
news:bdnanh$lv7$1$8302bc10@news.demon.co.uk...
> 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.
>
>
>
>





All times are GMT. The time now is 05:27 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.