看板 C_and_CPP 關於我們 聯絡資訊
※ 引述《neigence ()》之銘言: : AfxBeginThread 的參數主要有2個 : 1. 要執行的函數 : 2. 參數 : 如果要執行的函數不是一個 global function 的話 : 那他必定要是個 static method?? <-- 可是 我想要推翻這個情況 : 我想要利用多型來讓他呼叫 我想要的method : 我連function pointer都用上了 還是束手無策 : class MyThread{ : public: : void run( void (Runnable::*funPtr)() ){ : AfxBeginThread(funPtr,NULL); <-----error1 : } : } : class Runnable{ : public: : virtual void run()=0; : } : class Tester : public Runnable{ : public: : void run(){ : //...do something : } : } : void main(){ : Runnable* r = new Tester(); : MyThread* thread = new MyThread(); : thread->run(Tester::run) <-----error2 : } : error1 : 無法將 void (__thiscall Runnable::*)() 轉成AFX_THREADPROC : error2 : 無法將 void(__thiscall Tester::*)()轉成 : void (__thiscall Runnable::*)() : 如果定static 方法 則就不用去管這些問題...>"< 可是 我就想用多型嘛.. 那就定義一個static的方法啊~~ AfxBeginThread()需要一個static function,可以帶參數LPVOID 通常LPVOID是直接丟this進去~~ 然後再處理成 this->run(); 去呼叫該物件的member function。 例如 class A { static UINT threadEntryPoint(LPVOID p); virtual void run() = 0; }; class B : public A { virtual void run(); ///< 提供實作 }; -- 格局決定了結局 個性決定了命運 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.120.59.242
neigence:哈 謝謝 這方法的確好~~~ 10/06 14:32