I have a rather unusual problem… we are tasked with mapping a series of hexadecimal values to function pointers. However, I want to be able to call the function with already predefined parameters. Here is a segment of code that I am trying to get to work:
map<short, void(*)(MyClass, MyClass)> mapping = create_map();
map<short, void(*)(MyClass, MyClass)> create_map()
{
map<short, void(*)(MyClass, MyClass)>> m;
m[1] = &function(new MyClass(), new MyClass());
return m;
}
However, it will only let me put function without parameters into the mapping. How do I store this setup and call it later?
If you are using C++11, You can use
std::bind:If not, you can create a struct that stores the arguments and the function pointer. Then write an
operator()which will call the function through the function pointer with the arguments it stored: