typedef void (FunctionSet::* Function)(); class MyFunctionSet : public FunctionSet { protected: void addFunctions() { addFunction(Function(&MyFunctionSet::function1)); } void function1() { // Do something. } };
The addFunction method adds the function to a list in the base class,
which can then be enumerated to call all functions.
Is there any way to simplify (less typing work) the adding of functions?
Looks like you assign a member function pointer to a function of the derived class to a member function pointer to a function of the base class. Well, that’s forbidden, because it opens up a hole in the type-system. It comes at a surprise (at least for me, the first time i heard that). Read this answer for why.
To answer your actual question – i would make
addFunctiona template:Change
addFunctionin the base-class to this:Better use
static_cast, because it will tell you ifDerivedisn’t actually derived fromFunctionSet.