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
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.
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.
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.
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.
Re: Help - how to use switch to declare variable?
Quote:
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.
Re: Help - how to use switch to declare variable?
Quote:
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.
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.
Re: Help - how to use switch to declare variable?
Quote:
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.
Re: Help - how to use switch to declare variable?
Thanks for the additional information and suggestions.
BJT