Boost logo

Boost Users :

From: Kirit Sælensminde (kirit.saelensminde_at_[hidden])
Date: 2007-10-27 03:15:37


Golan Trevize wrote:
> // the callback function
> string add5(string x)
> {
> int v;
> v =atoi(x.c_str()) + 5;
> string vystup = (char *)v;
> return vystup;
> }

I'm not sure about the regex problem, but I think that the callback
doesn't do what you think it does.

The first part does convert the number in x into its value + 5 in v, but
the the second part doesn't convert v into a string.

I think Boost.Lexical cast will help here, or you can use a
std::stringstream:

std::stringstream ss;
ss << v;
string vystup = ss.str();

You used a C style cast, (char *)v, which is generally a pretty bad idea
in C++. The C++ casts (static_cast, dynamic_cast etc.) have better type
safety than the C ones. By using a C cast you've turned off the type
checking, but the type checker was trying to tell you something about
the expression - shutting it up doesn't make the problem go away.

K


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net