Re: Replacement for "switch" that can use non-const values?
On Aug 13, 7:10 am, "int2...@gmail.com" <int2...@gmail.com> wrote:
On Aug 12, 9:15 pm, "Jim Langston" <tazmas...@rocketmail.com> wrote:
Yes, that's exactly what I'd like to do. Except that I need pointers
to member functions, which gets complicated (at least for me). I can't
come up with a clean setup.
All hints would be appreciated.
A switch simply replaces an if...else block.
Yes, but IMHO that's very ugly and repetitive.
It's actually better structured than the switch you started
with. It's definitly the classical solution.
If not, you can always use a map:
class DoIt
{
public:
virtual ~DoIt() {}
virtual void operator()() const = 0 ;
} ;
std::map< int, DoIt const* > myMap ;
But you have to define a separate derived class for each case,
and initialize the map somehow. It may be appropriate, but it
does a lot more than a switch, and if a switch was fully
appropriate, then the chained if/else if would probably be more
appropriate. (On the other hand, if the switch was being used
simply because in C, you didn't have any other mechanism, and
each case of the switch is just a single function call, then the
idiom using a map might be more appropriate. Done correctly,
there is no central point where all of the cases are enumerated.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34