- 论坛徽章:
- 2
|
void (T::* _function)(); T的成员函数指针。
T* _pobject; T的指针。
ThreadParam(void (T::* function)(), T * pobject) ThreadParam的构造函数。
void (T::* function)(), 参数1, T的成员函数指针。
T * pobject , 参数2, T的指针。
(p->_pobject->*p->_function)();
对 _pobject指向的对象, 调用_function指向的函数。
例子 :
class Greeting {
std::string name_;
public:
explicit Greeting(const std::string& name) : name_(name) {}
void Hello() const { std::cout<<"hello "<<name_<<"\n"; }
}
void f() {
void ( C::* function)() = &C::hello;
C c("Cherrie");
(c.*function)(); // 对象使用 .* 运算符, 类似于 c.Hello(); 输出 Hello Cherrie
C& rc = c;
(rc.*function)(); // 引用使用 .* 运算符, 类似与 rc.Hello(); 输出同上。
C* pc = new C("OwnWaterloo");
(pc->*function)(); // 指针使用 ->* 运算符, 类似于 pc->Hello(); 输出 Hello OwnWaterloo
}
void g() {
std::auto_ptr<C> pc( new C("win32") );
ThreadParam<C>* pparam = new ThreadParam<C>( &C::hello, pc.get() );
pc.release(); // 要给ThreadParam添加析构行为, 以管理T*。 或者在WorkerThreadProc中 delete C*。
HANLDE thread = reinterpret_cast<HANLDE>( _beginthreadex(...
,reinterpret_cast<unsigned (__stdcall*)(void*)>( reinterpret_cast<ptrdiff_t>(&WorkerThreadProc<C>) )
,pc, .... );
CloseHandle(thread);
}
[ 本帖最后由 OwnWaterloo 于 2009-4-14 22:19 编辑 ] |
|