Click here to Skip to main content
15,887,683 members
Articles / Programming Languages / C++
Tip/Trick

Get a Pointer to a Memberfunction

Rate me:
Please Sign up or sign in to vote.
1.00/5 (1 vote)
25 Feb 2011CPOL 18.9K   2   16
This shows you how to cast a Pointer to a Memberfunction to any Pointer Type you whish.
In my actual project, I needed a way to get a void* to a Memberfunction but the compiler would just throw cast errors at me. So I just used the way in which the compiler implemented these calls.

void* p;
__asm MOV eax,offset MyClass::MyFunction
__asm MOV p,eax


or as Macro:
#define MEMBERPTR(f,v) __asm MOV eax,offset f \
                         __asm MOV v,eax


This will give you the correct address of virtual and normal member functions.

Tested on MSVC++ 2008.
Might be different on other Compilers!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 1 Introduces a concept which intention... Pin
damnedyankee24-Feb-11 4:58
damnedyankee24-Feb-11 4:58 
GeneralRe: /*So this is you final answer? Then could you provide me wit... Pin
Ansas Bogdan24-Feb-11 6:13
Ansas Bogdan24-Feb-11 6:13 
GeneralFAIL !!!! Pin
damnedyankee23-Feb-11 23:16
damnedyankee23-Feb-11 23:16 
GeneralRe: FAIL !!!! Pin
Ansas Bogdan24-Feb-11 2:35
Ansas Bogdan24-Feb-11 2:35 
GeneralRe: FAIL !!!! Pin
damnedyankee24-Feb-11 2:36
damnedyankee24-Feb-11 2:36 
GeneralRe: FAIL !!!! Pin
Ansas Bogdan24-Feb-11 2:40
Ansas Bogdan24-Feb-11 2:40 
GeneralRe: FAIL !!!! Pin
damnedyankee24-Feb-11 3:19
damnedyankee24-Feb-11 3:19 
GeneralRe: FAIL !!!! Pin
Ansas Bogdan24-Feb-11 3:58
Ansas Bogdan24-Feb-11 3:58 
GeneralRe: FAIL !!!! Pin
damnedyankee24-Feb-11 4:09
damnedyankee24-Feb-11 4:09 
GeneralRe: FAIL !!!! Pin
Ansas Bogdan24-Feb-11 4:45
Ansas Bogdan24-Feb-11 4:45 
GeneralRe: FAIL !!!! Pin
nv325-Feb-11 0:04
nv325-Feb-11 0:04 
GeneralRe: FAIL !!!! [modified] Pin
Ansas Bogdan25-Feb-11 1:51
Ansas Bogdan25-Feb-11 1:51 
General"So why dont use something when it works" Pin
imagiro25-Feb-11 4:48
imagiro25-Feb-11 4:48 
GeneralRe: "So why dont use something when it works" Pin
Ansas Bogdan25-Feb-11 5:02
Ansas Bogdan25-Feb-11 5:02 
Generallibsigc++ Pin
Tomaž Štih23-Feb-11 20:23
Tomaž Štih23-Feb-11 20:23 
GeneralRe: libsigc++ Pin
Ansas Bogdan24-Feb-11 2:49
Ansas Bogdan24-Feb-11 2:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.