Re: Help to remove reinterpret_cast from c++ code
On 24/04/2013 01:15, nvangogh wrote:
so how can this be re-written so there are no re-interpret casts?
This code actually looks safe to me - it's one of the cases where the
cast is doing exactly what you need. And the reason why you need it is
down to the interface into the OS not being templated :)
This is what MS say on the subject: "The result of a reinterpret_cast
cannot safely be used for anything other than being cast back to its
original type. Other uses are, at best, nonportable.". Your use is one
of the safe ones.
Your call is effectively this line:
pthread_create(&thread, 0, &cJoystick::loop, this);
This creates a thread, passing it a pointer to the static function
cJoystick::loop (which must have a signature "void name(void*)) and a
pointer to the data item that gets passed into it. In your code the
pointer is a pointer to a cJoystick, and is automatically cast to
void*.
The OS code then calls your supplied static function, passing it the
void pointer to your parameter - which is always a cJoystick.
If your parameter wasn't a cJoyStick it would most probably crash.
As a side issue (and it most likely will make no difference to the
compiled code) I'd write the routine like this. It gives you a place
you can stop the debugger, and look at your cJoystick object.
void* cJoystick::loop(void *obj)
{
cJoystick* pThis = reinterpret_cast<cJoystick *>(obj);
while (pThis->active)
{
pThis->readEv();
}
}
Andy
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]