Skip to main content
GameDev.net gamedev.net
🔒 Locked

Class Member Function

Started by Dark Rave May 20, 2002 at 3:04 PM 14 replies 250+ views
Original Post
Dark Rave
Dark Rave
Short question: How can I receive the virtual address of a class member function??? The result should be DWORD !! Thx for your answers
Fruny
Fruny
Short answer:
Use &Class::Function. The resulting type is (ReturnType)(Class::*)(ArgType1, ArgType2...)

Member function pointers are not regular pointers (which in turn are not DWORDs), unless the member function is static. There is no such thing as a DWORD in C++. Begone Microsoft spawn.

See also these recent discussion threads :
STL mem_fun1_t
Win32 & OOP
C++ and the dreaded Function Pointer

As well as your favorite C++ reference.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Dark Rave
Dark Rave
Ok at first thx for your answer, but it wasn´t that what i´m searching for.

I need the virtual address of the function, like it is shown in the watch window from VCPP. ( for example: &Class::MemberFunction = 0x040f0450 )

So how do I cast (&Class::MemberFunction) into an unsigned integer, which represents the virtual address of the function.


Thx for help
Fruny
Fruny
quote:
Original post by Dark Rave
So how do I cast (&Class::MemberFunction) into an unsigned integer, which represents the virtual address of the function.



You can''t do it.

The debugger actually has knowledge about the executable.
You could parse the executable''symbol table (if it has not been stripped) to find that out.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Dark Rave
Dark Rave
It must work!!! It also works with non-member functions, so why shouldn´t it work with member functions? They are also located at the virtual memory, like non-member functions. Logically there must be a low level function to get the address?

And how does it make the executable source??? The programm must have the address to the function, no matter if it is a member or non-memebr function.

So i don´t think, that the debugger only knows where the address of the function is.





[edited by - Dark Rave on May 20, 2002 5:21:52 PM]
Fruny
Fruny
Ok, using Win32 API functions (i.e. outside of the C++ language itself), have a look at GetProcAddress and GetModuleHandle and see if you can make them work for class member functions in the current executable.

Edit: Why doesn't it work ? Because member function pointers are not pointers.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


[edited by - Fruny on May 20, 2002 5:25:13 PM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Xai
Xai
If you are wanting what I think you want it can be done ... but i''m at work and don''t remember for sure how to do it ... i''ll try to post again tonight.

As for the cast part ... simply use reinterpret_cast(yourfunctionaddresshere) ... and you will get the pointer cast as a DWORD (which works on intel Win32 machines - NOT on alphas - since they use 64 bit addresses).


Dark Rave
Dark Rave
To Anonymus:
If i do so, i can´t cast the resulting member function pointer to an unsigned integer (or better: i found no way to cast, so this would be what i like to know )

UINT Address = (UINT)(&Class::Function); // doesn´t work

But i think their must be a way to do this.
Do you know how?



Thx
Dark Rave
Dark Rave
@Xai:

When I´m trying to cast this member-function pointer with reinterpret_cast into DWORD, my Compiler produces an internal compiler error 1001

Any solutions?

[edited by - Dark Rave on May 20, 2002 5:44:16 PM]
Pyabo
Pyabo
Hehe... what compiler are you using? Any time a Microsoft compiler spits out the messge "Internal Compiler Error" that is a compiler bug, not your fault.

You should be able to cast the address of a member function, or even just dump the address value using printf, but having that address is pretty much meaningless. You can''t treat it like a function pointer.
Dark Rave
Dark Rave
hehe ... printing it, results also in an internal compiler error. ok time for a new compiler update.

[edited by - Dark Rave on May 20, 2002 6:29:58 PM]
Fruny
Fruny
quote:
Original post by Dark Rave
hehe ... printing it, results also in an internal compiler error. ok time for a new compiler update.



g++ will let you do the cast from a PMF to a regular pointer (or an int), but it is a language extension, not standard C++.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Dark Rave
Dark Rave
Is it possible to write this language extension for Visual C++ ???
DerekSaw
DerekSaw
Maybe put a member function in a class... and then use the pointer to the class to obtain the function in it. Not sure whether it works for static function. Must try it out 1st...


(edit)
oops... should work with static. not sure about member function. i think my brain got some funny exception thrown.

[edited by - DerekSaw on May 22, 2002 11:46:56 AM]
"after many years of singularity, i'm still searching n the event horizon" o

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.