Thank PJH very much.The website given by you solute my problem.
And I'm pleased to share another solutions from a Chinese mailing list about
C/C++ (I tanslated it into English but I hope you will forgive my bad English):
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
C++ compiler is very strict to member functions。Almost all the compilers do not
allow to copy the address of a member function to an integer variable or a
DWORD variable or another variable because of the "this" pointer.
To a given class:
class MemberCallDemo
{
public:
void __stdcall foo (int a) { printf ("In MemberCallDemo::foo, a = %d\n", a); };
};
Methods below will all make the compiling error: DWORD dwFooAddrPtr= 0;
dwFooAddrPtr = (DWORD) &MemberCallDemo::foo; /* Error C2440 */
dwFooAddrPtr = reinterpret_cast<DWORD> (&MemberCallDemo::foo); /* Error C2440 */
We only can copy the address to a same type function potiner:
void (__stdcall MemberCallDemo::*foo_ptr)(int) = &MemberCallDemo::foo; //Legal
Well,How can we get the address?
Here're two methos.
1。Use the asm language:DWORD dwFooAddrPtr = 0;
__asm
{
/* Some Chinese which I don't know how to tanslate. */
MOV EAX, OFFSET MemberCallDemo::foo
MOV DWORD PTR [dwFooAddrPtr], EAX
}
2。Use the union to practise deception on the compiler:
This is a very very cool method:template <class ToType, class FromType>
ToType union_cast (FromType f)
{
union
{ FromType _f;
ToType _t;
} ut;
ut._f = f;
return ut._t;
}
DWORD dwAddrPtr = union_cast<DWORD> (&YourClass::MemberFunction);
If anyone want the the original ,he or she can visit my windows live space
http://aikilis.spaces.live.com.
To: [email protected]
From: [email protected]
Date: Thu, 19 Aug 2010 08:34:25 +0100
Subject: Re: [c-prog] How to get the address of functions in the struct?
2010/8/18 .Aikilis <[email protected]>:
> But I don't know how to get the addres of a function in the struct.
http://www.parashift.com/c++-faq-lite/pointers-to-members.html
--
PJH
[Non-text portions of this message have been removed]