I have discussed the similar topic here.P JH gave me an good answer and I
found another solution.I copy them to you and hope it can help you:
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 blog:
http://aikilis.weblog.com/easycmailing-list%e4%b8%8a%e7%9a%84%e8%ae%a8%e8%ae%ba%e5%be%97%e5%88%b0%e4%b8%80%e4%b8%aa%e7%bb%93%e6%9e%84%e4%bd%93%e6%88%96%e7%b1%bb%e4%b8%ad%e7%9a%84%e5%87%bd%e6%95%b0%e7%9a%84%e5%9c%b0/
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
To: [email protected]
From: [email protected]
Date: Wed, 25 Aug 2010 15:23:08 -0400
Subject: [c-prog] using function pointers within a class?
I had built a set of linked-list functions and decided to put them
into a class. I am having some difficulty with using function
pointers -- specifically doing a sort.
Prior to moving to the class structure, I had:
// Calling procedure
byPath = (struct ENTRY *)listsort(origList, compare_Path);
// listsort
ENTRY *listsort(ENTRY *list, int(*compare)(void *, void *))
{
...
result = (*compare)(p,q));
...
}
// compare_Path
int compare_Path(void *p, void *q){}
THIS ALL WORKS CORRECTLY
---------------------------------
Now that I've moved the functions into a class, I can't get the sort
to work. I call it like this:
// Calling procedure
obj.sort(compare_Paths);
// listsort
void Entry::sort(int(*compare)(void *, void *)){}
// compare_Paths
int Entry::compare_Paths(void *p, void *q){}
I get an error at the calling procedure line: 'compare_Paths' was not
declared in this scope. I have included the Entry.h file and have the
declaration for compare_Paths in the Public: part of my class.
If I call it with
obj.sort(Entry::compare_Paths);
I get 2 errors:
No matching function for call to 'Entry::sort(<unknown type>)'
Candidates are void Entry::sort(int(*)(void*, void*))
I have also tried creating a sortPaths() function.
// Calling procedure
obj.sortPaths();
// sortPaths
void Entry::sortPaths()
{
sort(Entry::compare_Paths);
}
I get 2 errors:
No matching function for call to 'Entry::sort(<unknown type>)'
Candidates are void Entry::sort(int(*)(void*, void*))
Any assistance in getting this to work will be appreciated.
~Rick
[Non-text portions of this message have been removed]