Below is my code….
i had use an map here..instead of map i want to use 2-d array .
user will enter command code say 50 and corresponding function will be called that is registered like
to_do_commands.registerCommand(50,get_status);
full code below
#include "stdafx.h"
#include <assert.h>
#include <iostream>
#include <map>
#include <string>
using namespace std;
void ping(void)
{
cout << "ping command executed\n";
}
void get_status()
{
cout<<"get status executed";
}
class ToDoCommands
{
public:
typedef void (*FunctionPtr)();
typedef int Code;
void registerCommand(Code code,FunctionPtr);
void callFunction(Code);
private:
map<Code,FunctionPtr> func_map;
};
void ToDoCommands::registerCommand(Code code,FunctionPtr func_ptr)
{
func_map[code] = func_ptr;
}
void ToDoCommands::callFunction(Code code)
{
assert(func_map.find(code)!=func_map.end());
func_map[code]();
}
int main(int argc,char **argv)
{
ToDoCommands to_do_commands;
// to_do_commands.registerFunction(50,ping);
to_do_commands.registerCommand(10,ping);
to_do_commands.registerCommand(50,get_status);
to_do_commands.callFunction(50);
return 0;
}
If you want to avoid any STL-dependence in you header file, you could use the PIMPL idiom and use anything you want in your implementation file (e.g. std::map).
It could look like this:
I have disallowed copying and assignment of the class for simplicity, you can add those methods if you really need them.