Help - how to use switch to declare variable?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Help - how to use switch to declare variable?

  1. #1
    Join Date
    Mar 2010
    Posts
    71

    Help - how to use switch to declare variable?

    Hi,

    Could someone please tell me how to use switch to declare varialble? The simplified code is like this:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int n = 0;
    
        switch(n)
        {
            case 0:
                int i;
                break;
    
            case 1:
                long i;
                break;
    
            case 2:
                double i;
                break;
    
            default:
                break;
        }
    
        // Use i later
    }
    This code won't compile. It gives me conflicting declaration error messages.

    Thanks,
    BJT

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,250

    Re: Help - how to use switch to declare variable?

    Introduce blocks of scope, e.g.,
    Code:
    case 0:
        {
            int i;
            // ...
        }
        break;
    A possibly better option sometimes is to define functions that are called from within the switch.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Help - how to use switch to declare variable?

    It seems like you want the type of the variable i to change based on some parameter. There are ways to do this. The question then becomes, will the type of i be known at compile time, or not until runtime?

    If the type can be decided at compile time, then you probably want to read up on templates, since they would be the simplest way to solve this problem.

    If the type won't be known until the program is running, however, you'll need a union. Note, unions only work with C-compatible types, and they aren't type-safe. A more powerful alternative in C++ is a boost::variant, if you have access to Boost.

    In the event that the possible types can be related through a common base class (this excludes primitive types, obviously), then you can use inheritance polymorphism to solve this yet another way.

  4. #4
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Help - how to use switch to declare variable?

    What you're trying to do is not possible. If you declare a variable in the switch, it only exists in that switch, you can not use it later. C++ is a strongly typed language.

    Look up how to use unions, that is what you want to use in your code. Unions get around the fact that C++ is strongly typed.

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,250

    Re: Help - how to use switch to declare variable?

    Oh, my mistake: I missed the "Use i later" comment.

    Besides unions, it may be possible for Boost.Any to be used to solve this, but I am afraid that I am not familiar enough with it to say more.
    Last edited by laserlight; August 4th, 2010 at 01:29 PM.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Help - how to use switch to declare variable?

    Quote Originally Posted by laserlight View Post
    Besides unions, it may be possible for Boost.Any to be used to solve this, but I am afraid that I am not familiar enough with it to say more.
    Yes, but I don't think it would have any real benefits over boost::variant in this case. With any, you effectively need to keep trying boost::any_casts until you find one that works; variant stores an index indicating which type it actually is. You don't have direct access to that index though.

  7. #7
    Join Date
    Mar 2010
    Posts
    71

    Re: Help - how to use switch to declare variable?

    Quote Originally Posted by Lindley View Post
    It seems like you want the type of the variable i to change based on some parameter. There are ways to do this. The question then becomes, will the type of i be known at compile time, or not until runtime?

    If the type can be decided at compile time, then you probably want to read up on templates, since they would be the simplest way to solve this problem.

    If the type won't be known until the program is running, however, you'll need a union. Note, unions only work with C-compatible types, and they aren't type-safe. A more powerful alternative in C++ is a boost::variant, if you have access to Boost.

    In the event that the possible types can be related through a common base class (this excludes primitive types, obviously), then you can use inheritance polymorphism to solve this yet another way.
    This gives me more information than I expected, and it's very helpful. Thank you and everyone.

  8. #8
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Help - how to use switch to declare variable?

    I don't think boost::anything has any advantage over a built-in union in this case.

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Help - how to use switch to declare variable?

    Quote Originally Posted by ninja9578 View Post
    I don't think boost::anything has any advantage over a built-in union in this case.
    Depends on the specific problem, but I tend to agree that a union would be effectively equivalent to a boost::any here.

  10. #10
    Join Date
    Mar 2010
    Posts
    71

    Re: Help - how to use switch to declare variable?

    Thanks for the additional information and suggestions.

    BJT

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center