type conversion question

Discussion in 'C++' started by bwalker@musings.com, Jul 20, 2009.

  1. Guest

    I have a question about type conversion. In the following code, what
    would be the correct way to convert the variable idOut to an unsigned
    short reference? I know that I'm going to loose precision but that is
    okay.

    typedef int Id;

    bool function( Id& idOut, const std::string &name)
    {
    return obj->getXXX(name, (unsigned short&)idOut);
    }

    I should use reintrepret_cast<>, correct?

    Thanks.

    -brad w.
     
    , Jul 20, 2009
    #1
    1. Advertising

  2. James Kanze Guest

    On Jul 20, 7:21 pm, "" <> wrote:
    > I have a question about type conversion. In the following
    > code, what would be the correct way to convert the variable
    > idOut to an unsigned short reference? I know that I'm going to
    > loose precision but that is okay.


    > typedef int Id;


    > bool function( Id& idOut, const std::string &name)
    > {
    > return obj->getXXX(name, (unsigned short&)idOut);
    > }


    > I should use reintrepret_cast<>, correct?


    Only if you want undefined behavior.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jul 21, 2009
    #2
    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. luna
    Replies:
    1
    Views:
    6,838
  2. ibiza
    Replies:
    2
    Views:
    5,362
    ibiza
    Jan 27, 2006
  3. Chris
    Replies:
    2
    Views:
    21,356
    Chris
    May 11, 2006
  4. Michal Nazarewicz

    conversion from const type* to type* allowed?

    Michal Nazarewicz, Jan 4, 2007, in forum: C Programming
    Replies:
    13
    Views:
    573
    Eric Sosman
    Jan 5, 2007
  5. Replies:
    1
    Views:
    458
    Sylvester Hesp
    May 16, 2007
Loading...

Share This Page