The syntax for a static function pointer looks like this:
void (*FuncPtr)();
For a member pointer, you should use this syntax:
void (class::*FuncPtr)();
If your functions do not require functions to be member functions, this is much cleaner. Once you figure out what functions you need, the easiest way to type these functions is:
typedef void(*FuncPtrType)(); typedef void(Class::*MemberFuncPtrType)();
Now you can simply declare a stack with function pointers as follows:
std::stack <FuncPtrType> funcPtrStack; std::stack <MemberFuncPtrType> memberFuncPtrStack;
To get a pointer to a function, you simply use "&" as you would like to get the address for any other data type in C ++:
FuncPtrType funcPtr = &staticFunc; // Somewhere "void staticFunc()" is defined MemberFuncPtrType memberFuncPtr = &Class::MemberFunc; // Somewhere void "Class::MemberFunc()" is defined
To actually call function pointers, you must use the "*" operator to return data from a pointer (like any other data type in C ++). The only difficult thing is that for member functions they need a pointer to a class, which makes it very inconvenient to use. This is why I recommended using static functions to get started. Anyway, here is the syntax:
(*funcPtr)();
Having shown all this, the following code should now make sense:
std::stack <MemberFuncPtrType> memberFuncPtrStack;
source share