As the title, suggests, I have a class that needs to have a map of pointers to members of another class. Because the name of the class should not matter, I have made it a template class. The problem I am having is how to extract a function pointer from the map, and call it. Please consider my code:
template<typename T>
class PointerClass
{
public:
typedefdouble(T::*memberFunction)();
void addFunction(std::string funcName, memberFunction func)
{
funcMap.insert(std::make_pair(funcName, func));
//Lets say later in another function we extract it (put it here for convenience)
double(T::*handler)();
handler = funcMap["new function"];
//How do I call it? Thought it was something like this, but because templates are involved,
//I'm lost!
(this->*handler)();
}
private:
std::map<std::string, memberFunction> funcMap;
};
class MyClass
{
public:
MyClass()
{
pClass.addFunc("new function", &MyClass::getValue);
}
double getValue()
{
return 25;
}
private:
PointerClass<MyClass> pClass;
};
Ok, so should I have a void* as a member of the template class, so that I can pass the *this pointer of MyClass into it? I will probably have to define a method for that, because it is generally not a good idea to do Class1<this>class1, because the object is not fully constructed.