468,774 Members | 2,629 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,774 developers. It's quick & easy.

When to use various types of casts?


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
Jul 19 '05 #1
4 3704
Michael Wagner <mi*********@nc.rr.com> wrote in message
news:mi*******************************@news3-ge0.southeast.rr.com...

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

Jul 19 '05 #2

"Michael Wagner" <mi*********@nc.rr.com> wrote in message
news:mi*******************************@news3-ge0.southeast.rr.com...

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
Jul 19 '05 #3

"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:bh********@dispatch.concentric.net...
John Harrison wrote:
"Michael Wagner" <mi*********@nc.rr.com> wrote in message
news:mi*******************************@news3-ge0.southeast.rr.com...
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.

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
Jul 19 '05 #4
Gianni Mariani wrote:
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.

All pointers are not necessarily related.


That's not what John wrote.
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by PengYu.UT | last post: by
8 posts views Thread by Alex Vinokur | last post: by
1 post views Thread by CARIGAR | last post: by
2 posts views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.