Jeremy said:
So C++ has no simple way to concat a string and other variables? For
example, in other languages (using C++ syntax):
//--
String s("my string is");
String n("long.");
int i = 99;
s += i + n;
s += i.ToString( ) + n;
s += ((string) i ) + n;
//--
You can't do that in C++?
You can do everything in C++.
It's just that some things don't come 'out of the box'.
For some things you need to do a little bit of work by yourself.
But once you have done that you have that functionality:
Taken from the FAQ and modified a little bit
inline std::string stringify(int x)
{
std:
stringstream o;
if (!(o << x))
throw BadConversion("stringify(double)");
return o.str();
}
can be used as in:
std::string s( "my string is" );
std::string n( "long." );
int i = 99;
s += stringify( i ) + n;
You might also go to
http://www.boost.org and look up lexical_cast.
Basically it does the very same: convert whatever you give to it into
a string. Whatever you do with that string is then up to you.
I am reading a C++ book, and I was wondering why
the author never described how to concat vars (aside from the insertion
operator)... That seems weird.
It's very simple: First convert everything to a std::string, then concat
all the individual pieces.