simple question on type casting~

Discussion in 'C++' started by black(flashing vampire), Jan 6, 2006.

  1. hi all~

    i'm newbie to C++ string type and get confused when trying to convert
    an int into string, i just want my cute messagebox to popup mouse
    positions, here is my code:
    Code:
    MessageBox(NULL, m.lX, "Hey", MB_OK);
    as u guess m.lX is of type of int, any help ?
    
     
    black(flashing vampire), Jan 6, 2006
    #1
    1. Advertising

  2. black(flashing vampire)

    Jim Langston Guest

    "black(flashing vampire)" <> wrote in message
    news:...
    > hi all~
    >
    > i'm newbie to C++ string type and get confused when trying to convert
    > an int into string, i just want my cute messagebox to popup mouse
    > positions, here is my code:
    >
    Code:
    > MessageBox(NULL, m.lX, "Hey", MB_OK);
    > as u guess m.lX is of type of int, any help ?
    > 


    The way I do it is by using string streams to convert them to std::strings.

    #include <string>
    #include <sstream>
    template<typename T, typename F > T StrmConvert( F from )
    {
    std::stringstream temp;
    temp << from;
    T to = T();
    temp >> to;
    return to;
    }

    The use of this is fairly simple. You just need to specify what you want to
    convert to. The compiler figures out (somehow) what you're converting from.

    std::string MyString = StrmConvert<std::string>( MyInt );

    So in your specific case it would be:

    MessageBox( NULL, (StrmConvert<std::string>( m.lX )).c_str(), "Hey",
    MB_OK );

    or if you want to see what's going on:

    std::string TempStr = StrmConvert<std::string>( m.lX );
    MessageBox( NULL, TempStr.c_str(), "Hey", MB_OK );

    the c_str() converts a std::string into a constant c style string. So you
    could copy it to a c-style string even if you wanted (but not advised).

    char TempStr[100];
    strcpy( TempStr, (StrmConvert<std::string>( m.lX )).c_str() );
    MessageBox( NULL, TempStr, "Hey", MB_OK );

    But it's best to use std::strings when you can and not convert them to
    c-style strings. First off, are you positive that the number will fit in
    100 bytes?
     
    Jim Langston, Jan 7, 2006
    #2
    1. Advertising

  3. black(flashing vampire) wrote:
    > hi all~
    >
    > i'm newbie to C++ string type and get confused when trying to convert
    > an int into string, i just want my cute messagebox to popup mouse
    > positions, here is my code:
    >
    Code:
    > MessageBox(NULL, m.lX, "Hey", MB_OK);
    > as u guess m.lX is of type of int, any help ?
    > 
    >


    See the FAQ:
    http://www.parashift.com/c -faq-lite/misc-technical-issues.html#faq-39.1

    Rennie deGraaf
     
    Rennie deGraaf, Jan 7, 2006
    #3
    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. kevin
    Replies:
    11
    Views:
    5,816
    Andrew McDonagh
    Jan 8, 2005
  2. heyo
    Replies:
    3
    Views:
    937
    Dan Pop
    Apr 1, 2004
  3. pete
    Replies:
    4
    Views:
    809
    Dan Pop
    Apr 2, 2004
  4. Wally Barnes
    Replies:
    3
    Views:
    533
    Wally Barnes
    Nov 20, 2008
  5. Sosuke

    Up casting and down casting

    Sosuke, Dec 20, 2009, in forum: C++
    Replies:
    2
    Views:
    577
    James Kanze
    Dec 20, 2009
Loading...

Share This Page