ideas needed - how to do PHP-like switch with strings?

A

A

so in php you can write nicely:

switch (case)
{
case "a": break;
case "whatever": break;
case 0: break;
case 40: break;
}

in C++ all you can use is ordinal types like int and so on. so any ideas how
would one use strings as well? I use CBuilder 2009 so ideas may be also
biased toward that one if it has some specific class to do this.
 
V

Victor Bazarov

so in php you can write nicely:

switch (case)
{
case "a": break;
case "whatever": break;
case 0: break;
case 40: break;
}

in C++ all you can use is ordinal types like int and so on. so any ideas how
would one use strings as well? I use CBuilder 2009 so ideas may be also
biased toward that one if it has some specific class to do this.

Define enumerators with those values, then a function that converts a
string into those enumerators, then do


switch (enumerator_from_str(case))
{
case a_enumerator: ...
case whatever_enumerator: ...
case 0: ...
case 40: ...
default: ...
}

There is no problem really since you're not trying to use run-time
values in the 'case', only in the 'switch'. Otherwise, just write a
bunch of 'if-else-if' statements.

V
 
M

Marcel Müller

Victor said:
Define enumerators with those values, then a function that converts a
string into those enumerators, then do

switch (enumerator_from_str(case))
{
case a_enumerator: ...
case whatever_enumerator: ...
case 0: ...
case 40: ...
default: ...
}

No good advice. In fact you end up with the same problem in the
conversion function.

If you really want to speed up things you need an ordered lookup table
or something like that.


Marcel
 
V

Victor Bazarov

No good advice. In fact you end up with the same problem in the
conversion function.

How is it "the same"? If the OP wanted to use a switch statement, there
is no escape from using compile-time constants,
If you really want to speed up things you need an ordered lookup table
or something like that.

<shrug> I don't understand the basis of your objections. What prevents
one from doing that in the conversion function? There are no "ordered
lookup tables" for use in a 'switch' statement, AFAIK, you're stuck with
integrals, moreover with constants. Or do you know something I don't?
Care to demonstrate?

V
 
K

Kaar

How is it "the same"?  If the OP wanted to use a switch statement, there
is no escape from using compile-time constants,

I think the reply is trying to indicate that you will end up doing an
if-else-if in the function. This is bad, because on new additions, you
will have to change the if-else-if and then come back and change the
case. Eventually, this will become tedious and/or error prone and/or
long and/or ugly.
<shrug> I don't understand the basis of your objections.  What prevents
one from doing that in the conversion function?  There are no "ordered
lookup tables" for use in a 'switch' statement, AFAIK, you're stuck with
integrals, moreover with constants.  Or do you know something I don't?
Care to demonstrate?

V

My advice would be to program C++ and not try and program PHP in C++. C
++ has its reasons for not allowing switches on strings or characters.
So as in all other cases, think what might those reasons be instead of
trying to circumvent them. The use of enumerators is a better practice
(again when writing C++, other languages may actually welcome other
kinds of switches) and more conformant with the rest of the language
features, so instead of adapting the language to your design, adapt
your design to the language to be able to get the full power out of
it.

David
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top