Click to See Complete Forum and Search --> : How to cast LPVOID to my class type??


ppuniversal
February 20th, 2007, 08:17 AM
Hello,
I am making a thread program, in which i call :


hThread = CreateThread( NULL,
0,
(LPTHREAD_START_ROUTINE)(this->runThread),
this,
0,
&ThreadId);


in the constructor of my class.

Here hThread is the handle to the thread created.
And runThread is my thread function.

The 4th parameter passed is the pointer to my current object, i.e. "this".

The prototype of my runThread function is:

static DWORD WINAPI runThread(LPVOID param);



Now the problem is:

In the runThread function when I convert the "param" to my class type i.e.

MyClass ob = (MyClass)param;


it gives the following error on compilation:


error C2440: 'type cast' : cannot convert from 'LPVOID' to 'MyClass'


Please tell either how to overcome this problem or any other way of passing my object to the runThread function.
Pawan

alex_gusev
February 20th, 2007, 08:53 AM
MyClass *pOb = (MyClass*)param;


i.e. you always pass THE POINTER, not an object's instance

TheCPUWizard
February 20th, 2007, 12:41 PM
MyClass *pOb = (MyClass*)param;


Where possible, you should use the C++ casting constructs rather than the "C" style. Do research on static_cast, dynamic_cast, reinterpret_cast, const_cast.

ppuniversal
February 21st, 2007, 01:00 PM
Thanks, it worked for me.
Both replies gives the same thing, its great, an really works fine.
Thanks
Pawan