Click here to Skip to main content
16,004,587 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 19.8K   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 
Ansas, try to do your "trick" with a member function of a class that has a virtual base class and you will see why it fails. Your assumption that pointers to member functions have the same length as a simple void* is not correct. Therefore, your "trick" might lead to nasty crashes that are extremely hard to debug.

You should read for example this article, in order to understand what's going on:
Member Function Pointers and the Fastest Possible C++ Delegates

Quote from that article: "On almost all compilers, a member function pointer is bigger than a function pointer. Most bizarrely, in Visual C++, a member function pointer might be 4, 8, 12, or 16 bytes long, depending on the nature of the class it's associated with, and depending on what compiler settings are used!"

Therefore, I find the trick you are proposing a very dangerous technique. You should have researched to subject a little deeper.
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.