[solved] HookEntry style stub for C++ class method

2 posts / 0 new
Last post
salass00
salass00's picture
Offline
Last seen: 6 months 2 weeks ago
Joined: 2011-02-03 11:27
[solved] HookEntry style stub for C++ class method

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:

  1. #include <utility/hooks.h>
  2.  
  3. class MyClass {
  4. ULONG func1(APTR obj, APTR msg);
  5. ULONG func2(APTR obj, APTR msg);
  6. ULONG func3(APTR obj, APTR msg);
  7. };
  8.  
  9. typedef ULONG (MyClass::*MyClassHookMethod)(APTR, APTR);
  10.  
  11. ULONG MyClassHookEntry(struct Hook *hook, APTR obj, APTR msg) {
  12. MyClass *instance = (MyClass *)hook->h_Data;
  13. MyClassHookMethod method = (MyClassHookMethod)hook->h_SubEntry;
  14. return (*instance.*method)(obj, msg);
  15. }

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:

  1. hooktest.cpp: In function ‘ULONG MyClassHookEntry(Hook*, APTR, APTR):
  2. hooktest.cpp:13:54: error: invalid cast from type ‘ULONG (*)() {aka long unsigned int (*)()}’ to type ‘MyClassHookMethod {aka long unsigned int (MyClass::*)(void*, void*)}
  3. MyClassHookMethod method = (MyClassHookMethod)hook->h_SubEntry;
  4. ^

Any ideas how to solve this?

I could also use friend functions for the hooks but I would rather avoid that if possible...

salass00
salass00's picture
Offline
Last seen: 6 months 2 weeks ago
Joined: 2011-02-03 11:27
Re: HookEntry style stub for C++ class method

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.

Log in or register to post comments