When to use various types of casts?

Discussion in 'C++' started by Michael Wagner, Aug 18, 2003.

  1. 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
     
    Michael Wagner, Aug 18, 2003
    #1
    1. Advertisements

  2. Michael Wagner

    David White Guest

    Michael Wagner <> wrote in message
    news:...
    >
    > 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
     
    David White, Aug 18, 2003
    #2
    1. Advertisements

  3. "Michael Wagner" <> wrote in message
    news:...
    >
    > 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, Aug 18, 2003
    #3
  4. "Gianni Mariani" <> wrote in message
    news:bhpseh$...
    > John Harrison wrote:
    > > "Michael Wagner" <> wrote in message
    > > news:...
    > >
    > >>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
     
    John Harrison, Aug 18, 2003
    #4
  5. Michael Wagner

    Rolf Magnus Guest

    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.
     
    Rolf Magnus, Aug 18, 2003
    #5
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sathyaish
    Replies:
    2
    Views:
    711
    Sathyaish
    May 22, 2005
  2. Soren Kuula
    Replies:
    2
    Views:
    794
    Henry S. Thompson
    Dec 1, 2005
  3. Jeremy Cowles

    STD types vs C++ intrinsic types

    Jeremy Cowles, Aug 18, 2003, in forum: C++
    Replies:
    5
    Views:
    2,098
    Bob Jacobs
    Aug 19, 2003
  4. Howard
    Replies:
    3
    Views:
    624
    Siemel Naran
    Jul 22, 2004
  5. Steve Knight
    Replies:
    2
    Views:
    1,014
    Steve Knight
    Oct 10, 2003
  6. Ark Khasin

    sub-int types, casts, MISRA and RH's writings

    Ark Khasin, Nov 8, 2007, in forum: C Programming
    Replies:
    6
    Views:
    497
    Old Wolf
    Nov 10, 2007
  7. Kazik�
    Replies:
    4
    Views:
    1,752
    Jonathan Lee
    Jul 6, 2009
  8. john.swilting
    Replies:
    1
    Views:
    323
    -berlin.de
    Mar 27, 2007
Loading...