Correct. switch works only for integral types. Not floats, doubles, or objects. Just integers.
Keep in mind that if has a nesting limit <100, and that each else if is another nesting level.
What I do when I need to fork based on, say, C strings and I don't know that the number of strings won't grow over time, I do this:
char *cases[]={
"case 1",
"case 2",
"case 3",
"case 4",
//...
0
};
int *which=-1;
for (int a=0;which<0 && cases[a];a++)
//s is what would ideally be inside the parentheses in the switch statement
if (!strcmp(s,cases[a]))
which=a;
switch (which){
case 0: //"case 1"
//...
break;
case 1: //"case 2"
//...
break;
case 2: //"case 3"
//...
break;
case 3: //"case 4"
//...
break;
//...
}
What? Why would the compiler enforce a maximum nesting level of if() statements or anything else for that matter, unless it was a hard limitation of the compiler?
I sadly worked on a project where a single function had 128 if statements and 64 elses.
IMHO I think the above code is less useful than just a huge block of if-elses. If anything (pun not intended), I would do something like this:
What? Why would the compiler enforce a maximum nesting level of if() statements or anything else for that matter, unless it was a hard limitation of the compiler?
Okay, I probably should have said that some compilers may have that limitation. I do know that VC++ has it, and the limit is well below 100 levels.
Something else I've done when I knew that the number of options would be somewhat large is this: typedef std::map<constwchar_t *,ErrorCode(Interpreter::*)(Line &),wstrCmp> commandListType;