Moving to static_cast and reinterpret_cast from old C-style cast

Discussion in 'C++' started by Kobe, Feb 15, 2006.

  1. Kobe

    Kobe Guest

    Hi,

    if I need to convert a size_t to an int, in "older" C++ I'd write the
    following code (using C-like "casting"):

    <CODE>
    std::vector<...> v;
    int count = (int) v.size();
    // v.size() returns size_t
    </CODE>

    Now that modern C++ has different cast tools, how the above code should
    be changed? Should I use static_cast or reinterpret_cast ?

    e.g.

    int count = static_cast<int>(v.size());

    Moreover, sometimes I need to convert 32 bits unsigned ints (typedef
    unsigned int DWORD) into pointers.

    e.g.

    <CODE>
    // Old style C casting
    DWORD address;
    // address = ...
    AClass * myObject = (AClass *)address;
    </CODE>

    Should I use static_cast or reinterpret_cast in this case?

    Thanks in advance,
    K
     
    Kobe, Feb 15, 2006
    #1
    1. Advertising

  2. Kobe wrote:
    > if I need to convert a size_t to an int, in "older" C++ I'd write the
    > following code (using C-like "casting"):
    >
    > <CODE>
    > std::vector<...> v;
    > int count = (int) v.size();
    > // v.size() returns size_t
    > </CODE>
    >
    > Now that modern C++ has different cast tools, how the above code should
    > be changed? Should I use static_cast or reinterpret_cast ?
    >
    > e.g.
    >
    > int count = static_cast<int>(v.size());


    That should suffice. Beware, though, that if the value of 'v.size()'
    cannot be represented in an int, the behaviour is implementation-
    defined, and you may end up with a negative 'count'. Do you really need
    'count' to be 'int'?

    > Moreover, sometimes I need to convert 32 bits unsigned ints (typedef
    > unsigned int DWORD) into pointers.


    Are you sure they will fit?

    > e.g.
    >
    > <CODE>
    > // Old style C casting
    > DWORD address;
    > // address = ...
    > AClass * myObject = (AClass *)address;
    > </CODE>
    >
    > Should I use static_cast or reinterpret_cast in this case?


    Only 'reinterpret_cast' will do.

    V
    --
    Please remove capital As from my address when replying by mail
     
    Victor Bazarov, Feb 15, 2006
    #2
    1. Advertising

  3. Kobe

    Ben Pope Guest

    Kobe wrote:
    > Hi,
    >
    > if I need to convert a size_t to an int, in "older" C++ I'd write the
    > following code (using C-like "casting"):
    >
    > <CODE>
    > std::vector<...> v;
    > int count = (int) v.size();
    > // v.size() returns size_t
    > </CODE>
    >
    > Now that modern C++ has different cast tools, how the above code should
    > be changed? Should I use static_cast or reinterpret_cast ?
    >
    > e.g.
    >
    > int count = static_cast<int>(v.size());


    That should work with the obvious limitation.

    What's wring with:
    typedef std::vector<...> myContainer;
    myContainer v;
    myContainer::size_type count = v.size();

    > Moreover, sometimes I need to convert 32 bits unsigned ints (typedef
    > unsigned int DWORD) into pointers.


    <shudder/>

    > <CODE>
    > // Old style C casting
    > DWORD address;
    > // address = ...
    > AClass * myObject = (AClass *)address;
    > </CODE>
    >
    > Should I use static_cast or reinterpret_cast in this case?


    reinterpret_cast is the tool for the job.

    Preferably, don't, of course.

    Ben Pope
    --
    I'm not just a number. To many, I'm known as a string...
     
    Ben Pope, Feb 15, 2006
    #3
  4. Kobe

    Tomás Guest

    Kobe posted:

    > Hi,
    >
    > if I need to convert a size_t to an int, in "older" C++ I'd write the
    > following code (using C-like "casting"):
    >
    ><CODE>
    > std::vector<...> v;
    > int count = (int) v.size();
    > // v.size() returns size_t
    ></CODE>
    >
    > Now that modern C++ has different cast tools, how the above code should
    > be changed? Should I use static_cast or reinterpret_cast ?
    >
    > e.g.
    >
    > int count = static_cast<int>(v.size());
    >
    > Moreover, sometimes I need to convert 32 bits unsigned ints (typedef
    > unsigned int DWORD) into pointers.
    >
    > e.g.
    >
    ><CODE>
    > // Old style C casting
    > DWORD address;
    > // address = ...
    > AClass * myObject = (AClass *)address;
    ></CODE>
    >
    > Should I use static_cast or reinterpret_cast in this case?
    >
    > Thanks in advance,
    > K


    Here's what I'd do:

    Where ever you have a C-style cast, replace it with "static_cast".

    Then recompile it.

    If "static_cast" causes any errors, then change the ones that cause
    errors to "reinterpret_cast".

    Basically, "reinterpret_cast" can do everything thing that "static_cast"
    does, and more.

    -Tomás
     
    Tomás, Feb 15, 2006
    #4
    1. Advertising

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. Scott Brady Drummonds

    reinterpret_cast<>() v. static_cast<>()

    Scott Brady Drummonds, Jan 13, 2004, in forum: C++
    Replies:
    11
    Views:
    20,946
    Nick Hounsome
    Jan 20, 2004
  2. Peter
    Replies:
    7
    Views:
    516
    bjarne
    Aug 9, 2006
  3. Lucy Ludmiller

    reinterpret_cast or static_cast ?

    Lucy Ludmiller, Sep 3, 2006, in forum: C++
    Replies:
    2
    Views:
    445
    dasjotre
    Sep 7, 2006
  4. Pallav singh
    Replies:
    5
    Views:
    1,027
    James Kanze
    Dec 6, 2010
  5. Alex Vinokur
    Replies:
    1
    Views:
    612
Loading...

Share This Page