468,484 Members | 2,095 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,484 developers. It's quick & easy.

case statement,

Why can't I define variables in a case statement:

case 1:
float u = expression;
...
break;
Also, is it bad form to cast ints to enums? For example, one might want 11
to map to an enum element NOVEMBER.
Sep 15 '06 #1
4 2579
"vsgdp" <he***@null.comwrote in message
news:u8nOg.9542$cw.2165@fed1read03...
Why can't I define variables in a case statement:

case 1:
float u = expression;
...
break;
Also, is it bad form to cast ints to enums? For example, one might want
11 to map to an enum element NOVEMBER.
case 1:
{
float u = expression;
// ...
break;
}
case 2:
// ....
Sep 15 '06 #2
"Jim Langston" <ta*******@rocketmail.comwrote in message
news:qn************@newsfe04.lga...
"vsgdp" <he***@null.comwrote in message
news:u8nOg.9542$cw.2165@fed1read03...
>Why can't I define variables in a case statement:

case 1:
float u = expression;
...
break;
Also, is it bad form to cast ints to enums? For example, one might want
11 to map to an enum element NOVEMBER.

case 1:
{
float u = expression;
// ...
break;
}
case 2:
// ....
Sorry, missed the 2nd part of your question, re casting ints to enums.
Casting a number as a month, would be okay *some of the time*. What
happens, however, when the value is 13?

enum Months
{
January = 1,
February,
March,
// ...
December
}

int Month = (something).

Going from Months to Month wouldn't be a problem as such, as you can examine
the value. if ( Month 13 || Month < 1 ) { some error }.

But when you go into a variable declared as Months
Months EnumMonth = Month; // maybe some cast required

What re you going to do if Month is 13? In a switch statement it shouldn't
be that difficult as long as you remember to use default:

I go from ints to enums all the time when serializing my classes (writing
them to files or databases) but I ensure that the int value came from an
enum, not from some other form of input (such as user input) without
checking.

I may have understood your question wrong, however. You may wish to
rephrase it as to what it is you would actually want to do.
Sep 15 '06 #3
On Thu, 14 Sep 2006 18:19:53 -0700 in comp.lang.c++, "vsgdp"
<he***@null.comwrote,
>Why can't I define variables in a case statement:

case 1:
float u = expression;
...
break;
You cannot define variables in such a way that some code execution
paths jump around them, leaving it ambiguous whether they would be
defined or not. Oldy moldy switch/case is rather unstructured in
that regard.

If you want a local variable in a case, bracket it:

case 1: {
float u = expression;
...
}
break;
Sep 15 '06 #4
David Harmon wrote:
On Thu, 14 Sep 2006 18:19:53 -0700 in comp.lang.c++, "vsgdp"
<he***@null.comwrote,
Why can't I define variables in a case statement:

case 1:
float u = expression;
...
break;

You cannot define variables in such a way that some code execution
paths jump around them, leaving it ambiguous whether they would be
defined or not. Oldy moldy switch/case is rather unstructured in
that regard.
I think that's only the case when the declared variable is initialized.
So this:

switch(i)
{
case 1:
float u = 0;
break;

case 2:
break;

}

Requires a diagnostic, but if you remove the "= 0" part it would be ok.

Brian
Sep 15 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

26 posts views Thread by Joe Stevenson | last post: by
6 posts views Thread by deanfamily11 | last post: by
7 posts views Thread by Shapper | last post: by
3 posts views Thread by mark.irwin | last post: by
12 posts views Thread by rAinDeEr | last post: by
5 posts views Thread by Frederick Dean | last post: by
1 post views Thread by microsoft.public.dotnet.languages.vb | last post: by
22 posts views Thread by John | last post: by
9 posts views Thread by Robbie Hatley | last post: by
13 posts views Thread by Satya | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.