下面一段代码是网上的牛人写的为了实现线程函数访问类成员而实现的类。作者说比MFC的实现方法好象要好一点,但是我还没考证,呵呵,有时间验证一下或者大家帮我验证一下也可以啊
classbase;
typedef int(base::*fnCallBack)(void*p);
structcallback(void*param;fnCallBack *pfuc;base *pThis;};
classbase{
static intmyThreadfuc(void*p){
structcallback *p1=(structcallback *)p;
base *pthis=p1->base;fnCallBack *pfuc=p1->pfuc;void*param=p1->param;
inti=(pthis->*pfuc)(param);deletep;
returni;
}
public:
voidmyCreateThread(fnCallBack pfuc,void*param){
structcallback *p=new structcallback;
p.param=param;p.pThis=this;p.pfuc=pfuc;
::CreateThread(myThreadfuc,p);
}
virtual intmyCallBack(void*p){printf("It's base class.\n");return0;}
};
classderived:publicbase{
intmyCallBack(void*p){printf("It's derived class\n");}
};
voidmyCreateThreadImitate(fnCallBack fuc,void*p)
{
(*fuc)(p);
}
voidmain()
{
base p;char*param;
p.myCreateThread(&(base::myCallBack),param);
derived p2;p2.myCreateThread(&(base::myCallBack),param);
}
buffer的blogs