As long as you don't mind some restrictions, it's pretty easy to do. The easiest way to complete the task limits you to classes that come from one common base class. In this case, you can do something like this:
struct functor_base {
virtual bool operator()() = 0;
};
Then, obviously, you will need some specific classes derived from this base:
struct eval_x : functor_base {
virtual bool operator()() { std::cout << "eval_x"; }
};
struct eval_y : functor_base {
virtual bool operator()() { std::cout << "eval_y"; }
};
- :
functor_base *create_eval_x() { return new eval_x; }
functor_base *create_eval_y() { return new eval_y; }
, factory:
std::map<std::string, functor_base *(*)()> name_mapper;
name_mapper["eval_x"] = create_eval_x;
name_mapper["eval_y"] = create_eval_y;
(!) , :
char *name = "eval_x";
functor_base *b = name_mapper.find(name)();
(*b)();
delete b;
, , . , factory , , .