What I want to do is make a HookEntry style stub function that calls a method specified by pointer in hook->h_SubEntry on an object instance also specified by pointer in hook->h_Data.
This is the test code I've written for this so far:
#include <utility/hooks.h> class MyClass { ULONG func1(APTR obj, APTR msg); ULONG func2(APTR obj, APTR msg); ULONG func3(APTR obj, APTR msg); }; typedef ULONG (MyClass::*MyClassHookMethod)(APTR, APTR); ULONG MyClassHookEntry(struct Hook *hook, APTR obj, APTR msg) { MyClass *instance = (MyClass *)hook->h_Data; MyClassHookMethod method = (MyClassHookMethod)hook->h_SubEntry; return (*instance.*method)(obj, msg); }
Unfortunately for some reason g++ does not seem to like my casting of h_SubEntry to MyClassHookMethod. I can't really understand why though as their both pointer types.
The error g++ gives me is this:
hooktest.cpp: In function ‘ULONG MyClassHookEntry(Hook*, APTR, APTR)’: hooktest.cpp:13:54: error: invalid cast from type ‘ULONG (*)() {aka long unsigned int (*)()}’ to type ‘MyClassHookMethod {aka long unsigned int (MyClass::*)(void*, void*)}’ MyClassHookMethod method = (MyClassHookMethod)hook->h_SubEntry; ^
Any ideas how to solve this?
I could also use friend functions for the hooks but I would rather avoid that if possible...
Just found that sizeof() on MyClassHookMethod returns a size of 8 bytes so apparently it's more than just a simple pointer as I originally thought. In that case I will just need to use an extended Hook structure to fit the extra data in and problem solved.