This question is based upon: Calling C++ class methods via a function pointer
What i would like to do is to register a generic object member function to a module lower in the architecture, that can invoke an event callback.
What i need is being able to register any object type (generic), so i do not have to have a registration function for each type of object.
Example from 1:
typedef void(Dog::*BarkFunction)(void);
Then to invoke the method, you use the ->* operator:
(pDog->*pBark)();
Example in my code:
// application layer
class ObjectInHighterLayer
{
ObjectInHighterLayer()
{
xyz::registerEventhandler(this, eventCallback); // ??? how to register here ???
}
void eventCallback(int x)
{
}
}
// middleware layer or hal layer
static clientcallback clientCb = NULL;
namespace xyz {
typedef void (GENERIC_OBJECT_TYPE::*clientcallback)(int /*x*/); // ??? how to define callback type here ???
void registerEventhandler(clientcallback cb);
{
clientCb = cb;
}
void task()
{
// ... if event happend
callClients();
}
void callClients()
{
if(clientCb != NULL)
{
clientCb(3);
}
}
}