Re: Help to remove reinterpret_cast from c++ code
On 2013-04-23 17:15, nvangogh wrote:
Hi, I have been reading a class that I found online that creates an
object to represent a joystick. Now i recall reading that any use of
'reinterpret cast' is dangerous as it is highly machine dependent.
[...]
void* cJoystick::loop(void *obj)
{
while (reinterpret_cast<cJoystick *>(obj)->active)
reinterpret_cast<cJoystick *>(obj)->readEv();
}
[...]
pthread_create(&thread, 0, &cJoystick::loop, this);
[...]
The value of 'this' will be static_cast'ed into void*, so I see no
problem in cJoystick::loop static_cast'ing obj back into cJoystick*.
(I guess that, in case void* and cJoystick* have different layouts,
static_cast does the right thing but reinterpret_cast may not.)
void* cJoystick::loop(void *arg)
{
cJoystick* obj = static_cast<cJoystick*>(arg); // do it only once
while (obj->active)
obj->readEv();
}
or, you can define another non-static member function that executes
the loop, and call it from cJoystick::loop.
inline void cJoystick::readEvWhileActive()
{
while (active) readEv();
}
void* cJoystick::loop(void *arg)
{
static_cast<cJoystick*>(arg)->readEvWhileActive();
}
--
Seungbeom Kim
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]