c++ find() for map of member function
I have two classes with inheritance. I want to allow the classes to store their function in one map. How can I create the find function by the key? And will the inheritance of the function work (MyClass_2 does not have its own doSmth() function, it should add the doSmth() from MyClass)? My attempt is below.:
template<class T>
class Class1
{
typedef void(T::*MyFunction)();
map<string, MyFunction> functionMap;
public:
void addFunc(string funcName, MyFunction function) {
functionMap.insert(pair<string, MyFunction>(funcName, function));
}
bool findFunc(string key) {
typename map<string, MyFunction>::iterator it;
it = functionMap.find(key.c_str());
}
};
class MyClass {
Class1<MyClass> class1;
void addFunc() {
class1.addFunc("Func1"开发者_JAVA技巧, &MyClass::doSmth);
}
void findAndCallFunc(string key) {
class1.findFunc(key);
}
void doSmth();
};
class MyClass_2: MyClass {
Class1<MyClass_2> class1;
void addFunc() {
class1.addFunc("Func1", &MyClass_2::doSmth);
}
}
EDIT: I tested my programm. It works. My problem is that if I call fundAndCallFunc from an object of MyClass_2. It takes not the class1 of MyClass_2, but class1 of MyClass. What should I change?
As Chris says, your findFunc
should return the actual function:
MyFunction findFunc(string key)
{
const map<string, MyFunction>::const_iterator it = functionMap.find(key);
return it == functionMap.end() ? NULL : it->second;
}
Further, if you only store the function pointer to a member function, you lose track of the actual object (so you could just make your map static!). Perhaps you should also store the this
pointer of the object, i.e. make your map like this:
std::map<std::string, std::pair<T*, MyFunction> > functionMap;
In addFunc
you would say
functionMap.insert(std::make_pair(funcName, std::make_pair(this, function)));
Usage: Suppose it = functionMap.find("key")
. Then you could say:
MyClass * const p = it->second.first;
MyFunction f = it->second.second;
p->*f(); // invoke -- yes, it's operator->*() in action
You have two immediately obvious problems here:
your
findFunc
method just looks up the function, it doesn't do anything with it (attempt to call it or return the method pointer), and its declared as returningbool
rather thanMyFunction
-- not sure what you want it to doyour
doSmth
method is private in MyClass, so you can't access it in MyClass_2. You'll need to make it protected or public.
精彩评论