c++ 类回调函数

 

1. fastdelegate

  MyDelegate funclist[10];

// delegates are initialized to empty CBaseClass a("Base A"); CBaseClass b("Base B"); CDerivedClass d; CDerivedClass c;

// Binding a simple member function

 funclist[0].bind(&a, &CBaseClass::SimpleMemberFunction);

if (funclist[i]) {

// Invocation generates optimal assembly code.

 funclist[i](i, msg);

}

else

 {

 printf("Delegate is empty\n");

 };

 

2.用一个静态的成员函数,或者非成员函数.由其中转调用成员函数,

用宏.

typedef void (*pfnNotify)(CObject *,int ,char *);

//

setNotify((CObject *)this,Notify);

if(m_Notify) m_Notify(m_obj,...);

 

 

typedef void(*pclsCallback)(void *,int);



class A{
public:
    void Notify(){printf("A  Notify!\n");};
};

#define CALLBACKPROXY(Class,Func)    void callbackProxy(void *AClass,int ) \
{ \
    ((Class *)AClass)->Func(); \
};


CALLBACKPROXY(A,Notify)


class B{
public:
pclsCallback  cb;
};

int main(int argc, char* argv[])
{
    B b;
    b.cb=callbackProxy;
    b.cb((A *)(NULL),0);

    printf("Hello World!\n");
    return 0;
}


3.

#include <iostream>
#include <map>
using namespace std;

class Base
{
private:
    map <Base*,void (Base::*)()> subscriber;
public:
    void Print(){cout<<"Base"<<endl;}
    void AddSubscriber(Base* obj ,void (Base::*memf)())
    {
        subscriber.insert(pair<Base*,void (Base::*)()>(obj,memf));
    }
    void publish()
    {
        map<Base*,void (Base::*)()>::iterator it=subscriber.begin();
        for (;it!=subscriber.end();it++)
        {
            void (Base::*memf)()=it->second;
            (it->first->*memf)();
        }
    }
};
class A:public Base
{
public:
    void PrintA(){cout<<"A"<<endl;}
};
class B:public Base
{
public:
    void PrintB(){cout<<"B"<<endl;}
};
class C:public Base
{
public:
    void PrintC(){cout<<"C"<<endl;}
};

int main()
{
    Base base;
    base.AddSubscriber(new A(),(void (Base::*)())&A::PrintA);
    base.AddSubscriber(new B(),(void (Base::*)())&B::PrintB);
    base.AddSubscriber(new C(),(void (Base::*)())&C::PrintC);
    base.publish();
    system("pause");
}

 

 

 

4.使用union  vs2010通过

#include "stdafx.h"

#include "windows.h"

typedef void(__stdcall *lpfn)(LPCTSTR);


template <typename T>
union callBack
{
 lpfn lpfn1;
 typedef void(T::*lpClassFn)(LPCTSTR);
 lpClassFn lpfn2;
 callBack(lpClassFn _lpfn2){lpfn2=_lpfn2;};
 operator lpfn (){ return lpfn1;};
 
} ;

class A
{
 lpfn CallBack;
public:
 void setCallback(lpfn _CallBack)
 {
  CallBack=_CallBack;
 };
 inline void CallCallBack(LPCTSTR lpstr)
 {
  if(CallBack)
   (*CallBack)(lpstr);
 }
};


class B
{
public :

 inline void fncallback(LPCTSTR lpstr)
 {
  ::MessageBox(NULL,lpstr,L"test",MB_OK);
 }

};


int _tmain(int argc, _TCHAR* argv[])
{
 B b;

 A a;
 a.setCallback(callBack<B>(&B::fncallback));

 a.CallCallBack(_T("callback string!"));

 return 0;

}

 


5.gcc

# c++ 成员函数指针普通函数指针转换
QMAKE_CXXFLAGS += -Wno-pmf-conversions

 


#include "stdio.h"
#include <pthread.h>

#include "simplethread.h"

typedef void (* pcallbackfunc)(void * obj, void *parm);

typedef void *(* pthreadcallbackfunc)(void * obj);

class CallbackFunc
{
public:
//        void testp_func(void *parm)
//        {
//                printf("num is %d\n", parm);
//        }

         void *test_func()
        {

            test_func2();
            return NULL;
        }

        virtual void *test_func2()
        {
            printf("test\n");
        }

        void Run()
        {
        pthread_t tid;
        pthreadcallbackfunc pc= &CallbackFunc::test_func;
        pthread_create(&tid,NULL,pc,this);
        }


};

class CallbackFunc2:public SimpleThread
{
   void *ThreadFunc()
        {
            printf("testbbb\n");
            return NULL;
        }
};

int testmembercall()
{
       CallbackFunc obj;
       pcallbackfunc p = &CallbackFunc::test_func;
       //for(int i = 100; i < 104; i++)
       //          p(&obj, &i);

       obj.Run();

       CallbackFunc2 obj2;
       obj2.Run();

       getchar();
         return 0;
}

 

 



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回调函数是一种通过函数指针来实现的机制,允许将函数作为参数传递给其他函数,并在需要的时候执行这个函数。在C++中,回调函数可以在内或外定义。 引用提供了一个在内定义回调函数的示例代码。在代码中,A中定义了一个成员函数call_back作为回调函数,并在成员函数test中将该回调函数注册到B的对象b中。回调函数通过将A的this指针传递给对象b,实现对A成员变量的访问。 引用提供了一个在外定义回调函数的示例代码。在代码中,A中定义了一个成员变量p_call_back,用于存储回调函数的地址。在成员函数test中,回调函数call_back被注册到A的对象a中,并在执行回调函数时将对象指针传递给回调函数,实现对A成员变量的访问。 总结来说,回调函数可以放在内或外定义。在内定义回调函数时,可以通过this指针访问的成员变量。而在外定义回调函数时,需要将对象指针作为参数传递给回调函数,以实现对的成员变量的访问。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [C++成员函数当作回调函数同时传递this指针](https://blog.csdn.net/weixin_45416828/article/details/124074506)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [c++回调函数(函数指针)](https://blog.csdn.net/qq_42518941/article/details/111998136)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值