-
August 4th, 2010, 11:11 AM
#1
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
-
August 4th, 2010, 11:15 AM
#2
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.
-
August 4th, 2010, 11:21 AM
#3
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.
-
August 4th, 2010, 01:13 PM
#4
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.
-
August 4th, 2010, 01:25 PM
#5
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.
-
August 4th, 2010, 01:44 PM
#6
Re: Help - how to use switch to declare variable?
Originally Posted by laserlight
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.
-
August 4th, 2010, 07:02 PM
#7
Re: Help - how to use switch to declare variable?
Originally Posted by Lindley
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.
-
August 5th, 2010, 08:08 AM
#8
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.
-
August 5th, 2010, 08:15 AM
#9
Re: Help - how to use switch to declare variable?
Originally Posted by ninja9578
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.
-
August 5th, 2010, 09:34 AM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|