When to use various types of casts?

M

Michael Wagner

I do some Windows kernel programming, where what I need to pass to some
Kernel call is "void* Context". Sometime later, I will get that Conext
back. I want to pass a class pointer to this system class, and then pass
the void* back to the class when the kernel calls be back.

I am not clear which of the casts I really want to use in this case,
though I am pretty sure that I don't want dynamic casts or const casts.
Do I want static or reinterpret casts in this case?

Thanks,

Mike
 
D

David White

Michael Wagner said:
I do some Windows kernel programming, where what I need to pass to some
Kernel call is "void* Context". Sometime later, I will get that Conext
back. I want to pass a class pointer to this system class, and then pass
the void* back to the class when the kernel calls be back.

I am not clear which of the casts I really want to use in this case,
though I am pretty sure that I don't want dynamic casts or const casts.
Do I want static or reinterpret casts in this case?

For converting a void * to some type of data pointer, use static_cast.
static_cast does more checking, and will actually do a conversion if
necessary, whereas reinterpret_cast just reinterprets the bit pattern as a
different, possibly completely unrelated, type. You would need
reinterpret_cast to convert an int * to a float *, for example, because
it's a conversion that makes no sense.

static_cast is therefore safer than reinterpret cast (but not necessarily
actually safe). Unless you know for certain that reinterpreting a bit
pattern rather than a conversion is what you want, then use static_cast. If
the compiler complains that it cannot do a static_cast (and you are still
quite sure that you want to go ahead with the cast) then use
reinterpret_cast.

DW
 
J

John Harrison

Michael Wagner said:
I do some Windows kernel programming, where what I need to pass to some
Kernel call is "void* Context". Sometime later, I will get that Conext
back. I want to pass a class pointer to this system class, and then pass
the void* back to the class when the kernel calls be back.

I am not clear which of the casts I really want to use in this case,
though I am pretty sure that I don't want dynamic casts or const casts.
Do I want static or reinterpret casts in this case?

Thanks,

Mike

Use static_cast for 'related' types, float to int for instance (since
they're both numbers). All pointers are related to void*, so your case is a
static_cast.

john
 
J

John Harrison

Gianni Mariani said:
All pointers are not necessarily related.

struct A {}; struct B {};

B * pb = 0;

A * pa = static_cast<A *>( pb );
// error: invalid static_cast from type `B*' to type `A*'

A * ypa = static_cast<A *>( (void *) 0 ); // legal

I didn't say all pointers are related, only all pointers are related to
void*. This does mean you could use two static_casts to get from any pointer
type to any other pointer type, but I don't think I'd recommend that.

I should have said all data pointers are related to void*. Function pointers
and member pointers are something else again.

john
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top