C++ Encapsulation question

Started by
3 comments, last by lucky6969b 13 years, 9 months ago
class myThread{public:   myThread() {  }   virtual ~myThread() { }    void run() { AfxBeginThread(HelloWindows95,NULL, NULL, NULL, NULL, NULL); } };inline UINT HelloWindows95(LPVOID pParam){   PlaySound(_T("HELLOWIN.WAV"), NULL, SND_FILENAME);   return 0;}





Just out of curiosity, is it possible to make the threadproc address "move" along as a new mythread object is created? As now HelloWindows95 is "pinned", if I
encapsulate HelloWindows95 into myThread (myThread::HelloWindows95), the proc would be 0xcccccccc at runtime, anyway I can wrap that around the class so that I could point to any proc i like which depends on the parameter passed in?
Thanks
Jack
Advertisement
Not sure if I interpreted your question correctly, but the way I read it you want to be able to create threads that start in different functions? If so, the easiest thing would probably be to pass a function pointer with the right signature to the constructor or run() function.

Passing it to run (for simplicity) might look like this:
class myThread{// ...  typedef UINT (*ThreadProc)(LPVOID);    void run(ThreadProc startAddr)  {    AfxBeginThread(startAddr, NULL, NULL, NULL, NULL, NULL);  }}
The other option, is something like:
class myThread{  virtual UINT proc()  {  }  static UINT HelloWindows95(LPVOID pParam)  {     myThread *pThread = (myThread)pParam;     return pThread->proc();  }    void run() { AfxBeginThread(HelloWindows95,this, NULL, NULL, NULL, NULL); }}

Then anything inheriting from "myThread" overrides the virtual "proc" function to get the functionality it wants.
Quote:Original post by KulSeran
The other option, is something like:
*** Source Snippet Removed ***
Then anything inheriting from "myThread" overrides the virtual "proc" function to get the functionality it wants.


Looks like it, thanks a lot
Quote:Original post by Windryder
Not sure if I interpreted your question correctly, but the way I read it you want to be able to create threads that start in different functions? If so, the easiest thing would probably be to pass a function pointer with the right signature to the constructor or run() function.

Passing it to run (for simplicity) might look like this:
*** Source Snippet Removed ***


Thanks

This topic is closed to new replies.

Advertisement