Although the previous answers are for your solution, your solution doesn't look like the correct one for your problem.
I have two suggestions: 1) Build a better abstract base class or 2) Use a base class for functors (function objects). Let's look at them in more detail...
Given a project of simulating a processor's instruction set, we can simulate the execution with:
struct Instruction_Interface
{
virtual void execute(void) = 0;
};
typedef std::vector<Instruction_Interface *> Instruction_Container;
//...
Instruction_Container::iterator iter;
Instruction_Container program_instructions;
//...
for (iter = program_instructions.begin();
iter != program_instructions.end();
++iter)
{
(*iter)->execute(); // Execute the instruction;
}
This allows calling of each instruction's execute
method, regardless of the kind of instruction. One can add more methods to the interface; in this example one could add "toString" which would convert the instruction to text.
Idea 2: Functors
Establish a base class or interface for the functions you want to use. Place them into a container and iterate over the container. The iteration can be conditional also:
struct Functor_Interface
{
virtual std::string get_name(void) const = 0;
virtual void execute(void) = 0;
virtual void execute_if_name(const std::string& name)
{ if (name == get_name())
{
execute();
}
}
};
typedef std::vector<Functor_Interface *> Functor_Container;
//...
Functor_Container the_functors;
//...
Functor_Container::iterator iter;
for (iter = the_functors.begin();
iter != the_functors.end();
++iter)
{
(*iter)->execute_if_name("loader"); // Execute the functor if it is a *loader*.
(*iter)->execute_if_name("math");
}
In summary, think over your design to see if there is a better process that doesn't require function names, but instead lets either the function decide to execute or generically executes blind methods.