关于成员函数指针和普通函数指针的转换

来源:互联网 发布:java直播平台源码 编辑:程序博客网 时间:2024/04/30 07:27

http://bbs.csdn.net/topics/10251675

查看九楼zdhe的回复。

a function call like following
p->func(100);   // it will return nothing.
in C++ will be compiled to like following

  push        64h   //push parameter one by one. c++ always use ESP to pass parameter, because ECX is used for 

//THIS pointer. there is no fastcall standard for C++
  mov         edx,p    
  mov         eax,dword ptr [edx]   //save vtable to eax
  mov         ecx,p    // __this call standard ask compiler to save class poiner to ecx.
  call        dword ptr [eax+4]    //it's func1 address, if must func, maybe [eax + 8],....


Have you got any idea?
so before get pointer , just do like following:

wait a common test func and see asmemble code ( for pi->play) , you will know shift 
[eax + n]   (n is waht you want .)

COM interface make sure this number will never change any more(else the caller program need rebuild ..hehe)

so 

_asm{
  mov         edx,pi    
  mov         eax,dword ptr [edx]   
  mov         eax , [eax + n]    //n is fix for you know.
}

then here, eax hold you p->Play ....

从汇编语言层面上证明了不可能直接进行转换。

不过boost::function 和 boost::bind 到底是怎么做到的需要追寻一下源码实现。

原创粉丝点击