- UID
 - 2198
 
 注册时间2005-6-29
阅读权限255
最后登录1970-1-1
副坛主 
    
 
 
 
该用户从未签到  
 | 
 
class Test 
{ 
public: 
        int m_ta; 
public: 
        int DataDataDec(int a) 
        { 
                return m_ta - a; 
        } 
        int DataDataDec() 
        { 
                return m_ta; 
        } 
}; 
 
class Func 
{ 
public: 
        int m_fa; 
        int m_fb; 
public: 
        Func(int a,int b):m_fa(a),m_fb(b){} 
        int GetDataSum() 
        { 
                return m_fa + m_fb; 
        } 
        int ff(int a,int b,int c){return 0;} 
}; 
 
 
typedef int (Test::*FUNC_TEST)(int); 
typedef int (Func::*FUNC_FUNC)(int); 
typedef int (Func::*FUNC_FUNC3)(int,int,int); 
typedef int (Func::*FUNC_FUNC0)(void); 
 
void bfuncc() 
{ 
        printf("Hello!\r\n"); 
} 
 
int __stdcall funcc(int a,int b,int c)  // 这里是Debug模式下 使用__stdcall声明,让函数自动完成栈平衡 
{ 
        if (a>b) 
        { 
                return a+c; 
        } 
        printf("funck~\r\n"); 
        return a-c; 
} 
 
int main(int argc, char* argv[]) 
{ 
        FUNC_TEST p = Test::DataDataDec; 
        Func ObjC(10,20); 
        cout<< ((Test * )&ObjC)->DataDataDec(5) <<endl; 
        cout<< (ObjC.*(FUNC_FUNC)Test::DataDataDec)(3) << endl; 
        cout<< (&ObjC->*(FUNC_FUNC)p)(2) << endl; 
        // 有this指针,有明确的函数地址,就可以实现调用 
        // 至于数据将如何处理,调用者必须自己了解内存结构 
 
        void * pp = funcc; 
        FUNC_FUNC3 ppp; //= (FUNC_FUNC3)pp; 
        memcpy(&ppp,&pp,4); 
        (&ObjC->*(FUNC_FUNC3)ppp)(12,15,13); // 无参的平衡不了堆栈 
        // __asm add esp,0x0c;  debug模式下会加入检测函数平衡机制 所以我们不能手动平衡 
 
        void * pp1 = bfuncc; 
        FUNC_FUNC0 ppp1; 
        memcpy(&ppp1,&pp1,4); 
        (&ObjC->*(FUNC_FUNC0)ppp1)(); 
 
        return 0; 
} 
 
执行效果: 
5 
7 
8 
funck~ 
Hello! 
Press any key to continue |   
 
 
 
 |