zoukankan      html  css  js  c++  java
  • c++_成员函数回调

    //---------------------------------------------------------------------------
    
    #include <vcl.h>
    #pragma hdrstop
    
    #include "Unit1.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    
    
    class CAppleDemo
    {
    public:
        CAppleDemo();
        ~CAppleDemo();
     
        void fun1(void);
     
        void fun2(int a, int b);
     
        int fun3(int a);
    };
    
    
    #include <iostream>
     
    CAppleDemo::CAppleDemo()
    {
          //    std::cout << "CAppleDemo::CAppleDemo()" << std::endl;
    }
     
     
    CAppleDemo::~CAppleDemo()
    {
           //    std::cout << "CAppleDemo::~CAppleDemo()" << std::endl;
    }
     
    void CAppleDemo::fun1(void)
    {
        ShowMessage( "CAppleDemo::fun1()" );
    }
     
    void CAppleDemo::fun2(int a, int b)
    {
            ShowMessage( "CAppleDemo::fun2() a="+(String)a+",b="+(String)b );
    }
    
    int CAppleDemo::fun3(int a)
    {
            ShowMessage( "CAppleDemo::fun2() a="+(String)a);
    
        return a;
    
    }
    
    template<typename dst_type, typename src_type>
    dst_type pointer_cast(src_type src)
    {
        return *static_cast<dst_type*>(static_cast<void*>(&src));
    }
     
    template<typename dst_type, typename src_type>
    dst_type union_cast(src_type src)
    {
        union{
            src_type s;
            dst_type d;
        }u;
        u.s = src;
        return u.d;
    }
    
    typedef void (__fastcall *FunFun1)(void* pThis, int edx);
    typedef void (__fastcall *FunFun2)(void* pThis, int edx, int a, int b);
    typedef int  (__fastcall *FunFun3)(void* pThis, int edx, int a);
    
    typedef void (__fastcall *FunFunMain)(void* pThis, TObject *Sender);
    
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
            : TForm(Owner)
    {
    }
    //---------------------------------------------------------------------------
    
    void __fastcall TForm1::FormCreate(TObject *Sender)
    {
      //  CAppleDemo cAppleDemo;
    
    
        //    FunFun1 fun1 = pointer_cast<FunFun1>(&CAppleDemo::fun1);
        //    FunFun2 fun2 = pointer_cast<FunFun2>(&CAppleDemo::fun2);
        //    FunFun3 fun3 = union_cast<FunFun3>(&CAppleDemo::fun3);
    
       //    fun1(NULL, NULL);
       //    fun2(NULL, NULL, 11, 22);
       //    fun3(NULL, NULL, 33);
    
            FunFunMain pM =  union_cast<FunFunMain>(&TForm1::Button1Click);
            pM(NULL,Sender);
    }
    //---------------------------------------------------------------------------
    
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
              ShowMessage("Button1Click");
    }
    //---------------------------------------------------------------------------
  • 相关阅读:
    jsonp跨域+ashx(示例)
    小菜学习Winform(六)剪切板和拖放复制
    小菜学习Winform(五)窗体间传递数据
    小菜学习Winform(四)MDI窗体(附示例)
    小菜学习设计模式(四)—原型(Prototype)模式
    docker常用命令
    confluence知识管理、团队协作软件
    摩拜单车模式优于OFO双向通信才能被认可
    爬虫解决网页重定向问题
    linux 7z 命令编译安装,mac安装p7zip
  • 原文地址:https://www.cnblogs.com/leochan007/p/13851887.html
Copyright © 2011-2022 走看看