Reinterpret Cast --- where to use it

P

Pallav singh

Reinterpret Cast
The reinterpret_cast is necessary here, because the compiler doesn't
have any information available about the relationship between the
types Y and Z.

class Y;
class Z;
Y *convert( Z * );
The problem arises when a programmer tries to force the issue;
ignorance is bliss only to a certain extent:
Y *convert (Z *zp)
{return reinterpret_cast<Y *>(zp); }

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++

can anyone give me scenario where we will be practically using it ?

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
N

Noah Roberts

Pallav said:
Reinterpret Cast
The reinterpret_cast is necessary here, because the compiler doesn't
have any information available about the relationship between the
types Y and Z.

class Y;
class Z;
Y *convert( Z * );
The problem arises when a programmer tries to force the issue;
ignorance is bliss only to a certain extent:
Y *convert (Z *zp)
{return reinterpret_cast<Y *>(zp); }

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++

can anyone give me scenario where we will be practically using it ?

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Any time you need to pass a C++ object through a win32 callback for one
example.
 
R

red floyd

Any time you need to pass a C++ object through a win32 callback for one
example.

Should't you use static_cast<> for that instead of reinterpret_cast<>?

e.g.:
extern "C" void callback(void *param)
{
T* p = static_cast<T*>(param);
// yada yada yada;
}

void set_callback(T* p)
{
void* callback_param = p
ApiFunctionWithCallback(&callback, callback_param);
}
 
P

peter koch

Should't you use static_cast<> for that instead of reinterpret_cast<>?

e.g.:
extern "C" void callback(void *param)
{
   T* p = static_cast<T*>(param);
   // yada yada yada;

}

void set_callback(T* p)
{
    void* callback_param = p
    ApiFunctionWithCallback(&callback, callback_param);
},

You are right: that conversion should be a static_cast. I did myself
wrongly believe that reinterpret_cast was to be used, believíng that
static_cast is for safe casts only. Unfortunately this is not so - but
Noah has my sympathy ;-).

/Peter
 
J

Juha Nieminen

Pallav said:
can anyone give me scenario where we will be practically using it ?

Assume that, for whatever reason, you need to print the bit pattern of
the internal representation of, for example, a value of type double.
I'll leave it as an exercise how to implement such a routine.
 
T

Tonni Tielens

can anyone give me scenario where we will be practically using it ?

For instance in network communication. Most low level network data
functions accept the address of a byte (mostly unsigned char) array
together with the size of that byte array. Eg. where you'll have a
double you want to send to the other side, you'll reinterpret_cast the
double pointer to a byte pointer and give the send() function that
byte pointer, together with the size of the double in number of bytes.
Doubles and bytes have no relationship. The compiler cannot convert
those pointers in a normal (static_cast) way. During runtime the byte
pointer must simply be created and exactly the same bit pattern as in
the double pointer must be stored in that byte pointer.
 
N

Noah Roberts

peter said:
You are right: that conversion should be a static_cast. I did myself
wrongly believe that reinterpret_cast was to be used, believíng that
static_cast is for safe casts only. Unfortunately this is not so - but
Noah has my sympathy ;-).

Except that you're wrong. Win32 doesn't use void* for its opaque types.
Check out the definition of LPARAM and WPARAM.

Maybe next time you wish to act high and mighty you should make sure you
know WTF you're talking about first.
 
N

Noah Roberts

red said:
Pallav said:
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
can anyone give me scenario where we will be practically using [reinterpret_cast] ?
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Any time you need to pass a C++ object through a win32 callback for one
example.

Should't you use static_cast<> for that instead of reinterpret_cast<>?

e.g.:
extern "C" void callback(void *param)

Change that to:
extern "C" void callback(long param)
{
T* p = static_cast<T*>(param);
// yada yada yada;
}

and this no longer works:
void set_callback(T* p)
{
void* callback_param = p
ApiFunctionWithCallback(&callback, callback_param);
}

You would use static_cast<> if, indeed, you were working with void* as
the opaque type. Win32 doesn't. WPARAM is an UINT_PTR, is an unsigned
int (note lack of * - not a typo). LPARAM is LONG_PTR is a long (again,
no *):

basetsd.h:

typedef _W64 long LONG_PTR, *PLONG_PTR;

windef.h:


typedef LONG_PTR LPARAM;

All your "DlgProc" functions look like this:

LRESULT CALLBACK MyDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM
lParam) {...}

Unfortunately, win32 programmers don't just get to modify the problem in
order to make static_cast<> possible (I wrap everything in a
boost::any*). This is why you'll find most people hate it (among many
other reasons). It's probably the worst GUI api that exists. C++
wrapper libraries like MFC or wxWindows...ANYTHING really...is
recommended for win32 development.
 
P

peter koch

Except that you're wrong.  Win32 doesn't use void* for its opaque types..
  Check out the definition of LPARAM and WPARAM.

Maybe next time you wish to act high and mighty you should make sure you
know WTF you're talking about first.

Did you have a bad start or what? I did not try to offend you, and
that language has no place here.
Also, whos talking about LPARAM or WPARAM? The discussion is about
callbacks: look at the code-example above and then have a look at e.g.
CreateThread. You will find that opaque parameters indeed are passed
in void pointers (LPVOID in Microspeak).

/Peter
 
N

Noah Roberts

peter said:
Did you have a bad start or what? I did not try to offend you, and
that language has no place here.
Also, whos talking about LPARAM or WPARAM? The discussion is about
callbacks: look at the code-example above and then have a look at e.g.
CreateThread. You will find that opaque parameters indeed are passed
in void pointers (LPVOID in Microspeak).

Sigh...nothing more frustrating than someone who responds to a thread
the haven't bothered following.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top