Click here to Skip to main content
15,897,891 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 19K   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 
I think you are missing the point. This isnt for aircraft autopilot code or anything amongst the lines. Its barely a way to circumvent compiler restrictions. It might not always be stable and there may be compilers where this trick doesnt work. The point is in VC 8.0 this actually works scince Memberfunction pointers are only "normal" pointers. Only the compiler would prevent you from casting it to a void*. So why dont use something when it works. I mean ofcourse for Programs where you need to be 100% sure that it will never fail and that it compiles with every compiler out there you shouldnt use this but as far as i can say this works with VC 8.0 if you have any example where this doesnt work then let me know. In fact there is barely any Situation where you actually need this. I needed this and i thought i would share my findings with others and that is what this Section is for:
[quote]Tips n tricks are meant to be very, very short tips, snippets or even just a one-liner you came across that saved your day[/quote]
If you dont like my solution just dont use it but declaring something as fail that actually works just because it cause its not supported and thus may not work on every compiler is just ...
And yes i am fully aware that you shouldnt do this but this doesnt stop me from doing so when i need it as long as it works! In fact doing things that shouldnt be done is how many things in history where developed. So dont deny something cause of the simple fact that you shouldnt do it.

And if you take a look at the Tags i set for this article youll see that they state C++, VC 8.0.
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.