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;
}