Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > simple question on type casting~

Reply
Thread Tools

simple question on type casting~

 
 
black(flashing vampire)
Guest
Posts: n/a
 
      01-06-2006
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 ?
 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      01-07-2006
"black(flashing vampire)" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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?


 
Reply With Quote
 
 
 
 
Rennie deGraaf
Guest
Posts: n/a
 
      01-07-2006
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-lit....html#faq-39.1

Rennie deGraaf
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
type(d) != type(d.copy()) when type(d).issubclass(dict) kj Python 5 12-26-2010 06:48 PM
#define ALLOCIT(Type) ((Type*) malloc (sizeof (Type))) Yevgen Muntyan C Programming 10 02-13-2007 02:52 AM
Inheritance Problem from a simple base type to a structured complicated type mflll@wiu.edu XML 0 07-27-2006 12:26 AM
Re: Type casting- a larger type to a smaller type pete C Programming 4 04-02-2004 05:19 PM
Re: Type casting- a larger type to a smaller type heyo C Programming 3 04-01-2004 06:35 PM



Advertisments