By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,197 Members | 956 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,197 IT Pros & Developers. It's quick & easy.

case statement,

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
"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

P: n/a
"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

P: n/a
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

P: n/a
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.