case statement,

V

vsgdp

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.
 
J

Jim Langston

vsgdp said:
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:
// ....
 
J

Jim Langston

Jim Langston said:
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.
 
D

David Harmon

On Thu, 14 Sep 2006 18:19:53 -0700 in comp.lang.c++, "vsgdp"
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;
 
D

Default User

David said:
On Thu, 14 Sep 2006 18:19:53 -0700 in comp.lang.c++, "vsgdp"


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top