• Advertisement
Sign in to follow this  

C++ Encapsulation question

This topic is 2775 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


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

Share this post


Link to post
Share on other sites
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);
}
}


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement