How to cast LPVOID to my class type?
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
# 1 Re: How to cast LPVOID to my class type?
Hi,
you need to de-reference param first.
try
(CMyClass)(*param);
or better if possible:
static_cast<CMyClass>(*param);
haven't tried second approach, though.
Cheers,
D
# 2 Re: How to cast LPVOID to my class type?
that is of course:
CMyClass myClass = (CMyClass)(*param);
but you knew that.
# 3 Re: How to cast LPVOID to my class type?
You need to do this:
MyClass ob = *(MyClass*)param;
i.e., cast to a pointer and then dereference the result.
And no, I'm not going to recommend the use of static_cast because I think it stinks.
Danny at 2007-11-11 21:00:54 >

# 4 Re: How to cast LPVOID to my class type?
Hi Danny,
that's interesting. Why do you think static_cast "stinks"?
What about the other casts (dyna, const and reinterpret)? The reinterpret stinks
to me, because it allows you to cast unrelated types into one another, but the rest?
Cheers,
D
# 5 Re: How to cast LPVOID to my class type?
In my opinion you probably need to work with the object passed to your thread procedure. In this case you can make another kind of conversion:
MyClass * const ob = (MyClass*)param;
or using static_cast or reinterpret_cast. Then you can access this object, for example:
ob->DoWork();
Otherwise you create a local copy of the "this" object, which probably you do not need.
I hope this makes sense.
Viorel at 2007-11-11 21:02:52 >

# 6 Re: How to cast LPVOID to my class type?
Hi Danny,
that's interesting. Why do you think static_cast "stinks"?
What about the other casts (dyna, const and reinterpret)? The reinterpret stinks
to me, because it allows you to cast unrelated types into one another, but the rest?
Cheers,
D
It's along story but I'll say it in brief:
first,I have nothing against dynamic_cast. It stands out from the rest of the cast operators and it's indispensable anyway.
Viorel, I know it's not nice to point a finger at people in the street, but I'm pointing at your latest post on this thread to show exactly why static_cast stinks. Most programmers rarely understand what the difference between static_cast and reinterpret_cast is, not realizing that they're absolutely not interchangeble. static_cast has some knowledge about the types it casts to so it performs some adjustments. For example, static_cast of int to float will completely change the binary layout of the result, to ensure that you get the same value. reinterpret_cast in this case will cause undefined behavior.
const_cast is pure nonsense. I'm not saying that isn't needed -- it surely is sometimes -- but no one really needs a special operator for it. Besides, sometimes you need to perform both static cast and const cast in one expression:
const unsigned char * s
(char *) s; //two casts actually
You could argue that programmers could make mistakes with C cast. The problem is confusing reinterpret_cast and static_cast could cause even more damage.
In short, casts are bad but unavoidable. The addition of three cast opeartors that perform what a single C-style cast operator does is jus a nuisance, with no really added value in terms of safety. In just forces programmers to type much more, breal lines of code and often, choose the wrong cast operator. It's stupid and useless.
Danny at 2007-11-11 21:03:52 >

# 7 Re: How to cast LPVOID to my class type?
It worked for me using
MyClass * const ob = (MyClass*)param;
I just made the change
MyClass *ob = (MyClass*)param;
Pawan
# 8 Re: How to cast LPVOID to my class type?
Why do you need the const here? It forces you to call only const member functions from obj. Sometimes this could be exactly what you want but are you sure about this?
Danny at 2007-11-11 21:05:54 >

# 9 Re: How to cast LPVOID to my class type?
Why do you need the const here? It forces you to call only const member functions from obj. Sometimes this could be exactly what you want but are you sure about this?
In my opinion notations like MyClass * const ob do not mean that only constant members can be accessed. It means that the local pointer variable cannot be changed. I think other sense have the const MyClass * ob and const MyClass * const ob declarations.
Viorel at 2007-11-11 21:07:02 >

# 10 Re: How to cast LPVOID to my class type?
Yes, you're right. It's actualy a const pointer, not a pointer to a const object.
Danny at 2007-11-11 21:07:58 >
