C++ Scope

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jonathan Clements

    #1

    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

    #2
    Re: C++ Scope

    [color=blue]
    > 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 */ }[/color]
    and[color=blue]
    > 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.
    >[/color]
    Your code is correct, even if some compilers incorrectly complain.


    Comment

    • Stuart Golodetz

      #3
      Re: C++ Scope

      "Jonathan Clements" <jonathan@psilo cybe.demon.co.u k> wrote in message
      news:bdnanh$lv7 $1$8302bc10@new s.demon.co.uk.. .[color=blue]
      > 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 */ }[/color]
      and[color=blue]
      > 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.[/color]

      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.
      [color=blue]
      > Thanks,
      >
      > Jon.[/color]


      Comment

      • Jonathan Clements

        #4
        Re: C++ Scope

        Thank you to Kurt K., John H., and Stuart G. for your replies...

        "Jonathan Clements" <jonathan@psilo cybe.demon.co.u k> wrote in message
        news:bdnanh$lv7 $1$8302bc10@new s.demon.co.uk.. .[color=blue]
        > 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 */ }[/color]
        and[color=blue]
        > 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.
        >
        >
        >
        >[/color]


        Comment

        Working...