// ThreadRunObject.cpp : Defines the entry point for the console application.
//
#include
"stdafx.h"
#include
"stdio.h"
#include
<process.h>
#include
<Windows.h>
//
怎样在线程中运行类的成员函数
1 ,
静态成员函数
2,
传结构
,
如下
class
Hack
{
private
:
int x;
public
:
int get_x();
static void func(Hack * pthis); //
静态成员函数
void pmf(); //
非静态成员函数
};
void
Hack::func(Hack * pthis)
{
int y = pthis->get_x(); //
访问对象的数据成员
}
int
Hack::get_x()
{
return x;
}
void
Hack::pmf()
{
int i = 0;
for(i = 0; i < 10; i++)
{
printf("nihao /n");
}
}
struct
A
{
Hack * p; //
类对象指针
void (Hack::*pmf)(); //
成员函数指针
};
void
intermediary(void* ptr)
{
A* pa=static_cast < A* > (ptr); //
强制转换
p
为
A*
Hack* ph=pa->p; //
从
A
中析取
Hack
对象地址
void
(Hack::*pmf)()=pa->pmf; //
析取
ptr
到成员函数
(ph->*pmf)(); //
调用成员函数
}
int
_tmain(int argc, _TCHAR* argv[])
{
A a; //
结构实例
Hack h; //
创建对象
//
填充结构
a.p = & h;
a.pmf = &Hack::pmf;
HANDLE hHandle;
hHandle = (HANDLE)_beginthread(intermediary,0,&a);
WaitForSingleObject(hHandle,INFINITE);
return 0;
}