#include <stdio.h>
typedef bool (*PMethod)(void* pContext);
PMethod g_pMethod = nullptr;
void* g_pContext = nullptr;
void RegisterCallback(PMethod pMethod, void* pContext)
{
g_pMethod = pMethod;
g_pContext = pContext;
}
void UnregisterCallback()
{
g_pMethod = nullptr;
g_pContext = nullptr;
}
void CallCallback()
{
(*g_pMethod)(g_pContext);
}
class CSampleClass
{
public:
CSampleClass()
{
RegisterCallback(CSampleClass::callback, this);
}
~CSampleClass()
{
UnregisterCallback();
}
private:
static bool callback(void* pContext)
{
// Check Context
if (pContext != nullptr)
{
CSampleClass* pSampleClass = static_cast<CSampleClass*>(pContext);
pSampleClass->call();
return true;
}
else
{
return false;
}
}
bool call()
{
printf("Callback successfully called\n");
return true;
}
};
int main(int, char**)
{
CSampleClass oSampleClass;
CallCallback();
}