Use of local variables in case statement

Discussion in 'C++' started by A, Sep 11, 2010.

  1. A

    A Guest

    My compiler complains (probably for a good reason) that it cannot use local
    variable in case statement.

    But it is a mess if I declare it outside case. So I would like to keep it
    local.

    Is there a way to override this compiler behaviour or to write code
    differently so that it doesn't trigger compiler error:

    For example:

    switch (condition)
    {
    case 0: // do something
    break;
    case 1: int i = 0; // This is where compiler says "case bypasses
    init of local variable"
    // use i here
    break;
    }
     
    A, Sep 11, 2010
    #1
    1. Advertising

  2. A

    A Guest

    hmm... looks like i solved this one myself by inserting braces on case 1.


    switch (condition)
    {
    case 0: // do something
    break;
    case 1: {
    int i = 0;
    // use i here
    }
    break;
    }

    Perhaps someone knows some other variation/solution to this?
     
    A, Sep 11, 2010
    #2
    1. Advertising

  3. A

    Jorgen Grahn Guest

    On Sat, 2010-09-11, Daniel T. wrote:
    > In article <i6gmjs$1b6$>, "A" <> wrote:
    >
    >> hmm... looks like i solved this one myself by inserting braces on case 1.
    >>
    >>
    >> switch (condition)
    >> {
    >> case 0: // do something
    >> break;
    >> case 1: {
    >> int i = 0;
    >> // use i here
    >> }
    >> break;
    >> }
    >>
    >> Perhaps someone knows some other variation/solution to this?

    >
    > You have the correct solution. You might want to seriously consider
    > putting the block that goes in the case statement into its own function:
    >
    > void case1func() {
    > int i = 0;
    > // use i here
    > }
    >
    > ...
    >
    > switch (condition) {
    > case 0:
    > case0func();
    > break;
    > case 1:
    > case1func();
    > break;
    > }


    Depends very much on what he's doing. If it's like most switches I
    see in real code, breaking it into many tiny functions would create a
    world of spaghetti pain.

    Do what makes sense for the problem at hand.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Sep 11, 2010
    #3
  4. A

    Öö Tiib Guest

    On 11 sept, 22:43, "A" <> wrote:
    > My compiler complains (probably for a good reason) that it cannot use local
    > variable in case statement.
    >
    > But it is a mess if I declare it outside case. So I would like to keep it
    > local.
    >
    > Is there a way to override this compiler behaviour or to write code
    > differently so that it doesn't trigger compiler error:
    >
    > For example:
    >
    > switch (condition)
    >     {
    >     case 0: // do something
    >             break;
    >     case 1: int i = 0;        // This is where compiler says "case bypasses
    > init of local variable"
    >             // use i here
    >             break;
    >     }


    Maybe like this:

    switch (condition)
    {
    default:
    assert( !"buggy switch" );
    break;
    case 0:
    // do something
    break;
    case 1:
    {
    int i = 0;
    // use i here
    }
    break;
    }
     
    Öö Tiib, Sep 11, 2010
    #4
  5. A

    Goran Pusic Guest

    On Sep 11, 10:42 pm, Jorgen Grahn <> wrote:
    > On Sat, 2010-09-11, Daniel T. wrote:
    > > In article <i6gmjs$>, "A" <> wrote:

    >
    > >> hmm... looks like i solved this one myself by inserting braces on case 1.

    >
    > >> switch (condition)
    > >>     {
    > >>     case 0: // do something
    > >>             break;
    > >>     case 1: {
    > >>             int i = 0;
    > >>             // use i here
    > >>             }
    > >>             break;
    > >>     }

    >
    > >> Perhaps someone knows some other variation/solution to this?

    >
    > > You have the correct solution. You might want to seriously consider
    > > putting the block that goes in the case statement into its own function:

    >
    > > void case1func() {
    > >    int i = 0;
    > >    // use i here
    > > }

    >
    > > ...

    >
    > > switch (condition) {
    > > case 0:
    > >    case0func();
    > >    break;
    > > case 1:
    > >    case1func();
    > >    break;
    > > }

    >
    > Depends very much on what he's doing.  If it's like most switches I
    > see in real code, breaking it into many tiny functions would create a
    > world of spaghetti pain.


    ;-)

    On the contrary, a function creates ravioli pain, not spaghetti pain!

    G.

    P.S. http://c2.com/cgi/wiki?RavioliCode
     
    Goran Pusic, Sep 13, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. karim
    Replies:
    1
    Views:
    776
    George Ter-Saakov
    Jun 26, 2003
  2. Sullivan WxPyQtKinter
    Replies:
    10
    Views:
    671
    Antoon Pardon
    Nov 8, 2007
  3. Tammo Tjarks
    Replies:
    2
    Views:
    289
    Tammo Tjarks
    Sep 13, 2007
  4. Replies:
    2
    Views:
    102
  5. Replies:
    2
    Views:
    143
Loading...

Share This Page