Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C++ Scope

Reply
Thread Tools

C++ Scope

 
 
Jonathan Clements
Guest
Posts: n/a
 
      06-29-2003
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.




 
Reply With Quote
 
 
 
 
Kurt Krueckeberg
Guest
Posts: n/a
 
      06-29-2003

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


 
Reply With Quote
 
 
 
 
Stuart Golodetz
Guest
Posts: n/a
 
      06-29-2003
"Jonathan Clements" <(E-Mail Removed)> wrote in message
news:bdnanh$lv7$1$(E-Mail Removed)...
> 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.



 
Reply With Quote
 
Jonathan Clements
Guest
Posts: n/a
 
      06-29-2003
Thank you to Kurt K., John H., and Stuart G. for your replies...

"Jonathan Clements" <(E-Mail Removed)> wrote in message
news:bdnanh$lv7$1$(E-Mail Removed)...
> 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.
>
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Lexical scope vs. dynamic scope Xah Lee Java 0 02-26-2009 10:08 AM
CSPEC issue: lossing scope (or incorrect scope) in cspec subroutine. balldarrens@gmail.com Perl Misc 0 02-05-2009 08:42 PM
Scope - do I need two identical classes, each with different scope? ann Java 13 09-13-2005 03:07 AM
How do namespace scope and class scope differ? Steven T. Hatton C++ 9 07-19-2005 06:07 PM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM



Advertisments