Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   When to use various types of casts? (http://www.velocityreviews.com/forums/t276824-when-to-use-various-types-of-casts.html)

Michael Wagner 08-18-2003 03:25 AM

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

David White 08-18-2003 04:04 AM

Re: When to use various types of casts?
 
Michael Wagner <mikepwagner@nc.rr.com> wrote in message
news:mikepwagner-F1B9B6.23253617082003@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




John Harrison 08-18-2003 05:51 AM

Re: When to use various types of casts?
 

"Michael Wagner" <mikepwagner@nc.rr.com> wrote in message
news:mikepwagner-F1B9B6.23253617082003@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



John Harrison 08-18-2003 08:38 AM

Re: When to use various types of casts?
 

"Gianni Mariani" <gi2nospam@mariani.ws> wrote in message
news:bhpseh$i32@dispatch.concentric.net...
> John Harrison wrote:
> > "Michael Wagner" <mikepwagner@nc.rr.com> wrote in message
> > news:mikepwagner-F1B9B6.23253617082003@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



Rolf Magnus 08-18-2003 09:33 AM

Re: When to use various types of casts?
 
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.




All times are GMT. The time now is 10:14 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.