468,512 Members | 1,501 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

perferred way of converting const char * to const std::string &

Just looking for opinion on which of the 3 methods below people use in
their code when they convert a 'const char *' to a 'const std::string
&'

came across #3 in someone's code and I had to think for a sec. At
first I read it as converting a 'const char *' to a 'std::string *'

void f(const std::string &s)
{
std::cout << s.size() << "\n";
}

int main()
{
const char *c="sal";

f(c); //1
f(std::string(c)); //2
f(static_cast<std::string>(c)); //3

return 0;
}

Aug 9 '06 #1
2 9472
po**************@yahoo.com wrote:
Just looking for opinion on which of the 3 methods below people use in
their code when they convert a 'const char *' to a 'const std::string
&'

came across #3 in someone's code and I had to think for a sec. At
first I read it as converting a 'const char *' to a 'std::string *'

void f(const std::string &s)
{
std::cout << s.size() << "\n";
}

int main()
{
const char *c="sal";

f(c); //1
f(std::string(c)); //2
f(static_cast<std::string>(c)); //3

return 0;
}
(1) is the most common.

(2) is only needed when you have 'f' overloaded and must pick one with
'std::string' as its argument.

(3) is the same as (2), IMO.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 9 '06 #2
<po**************@yahoo.comschrieb im Newsbeitrag
news:11**********************@75g2000cwc.googlegro ups.com...
Just looking for opinion on which of the 3 methods below people use in
their code when they convert a 'const char *' to a 'const std::string
&'

came across #3 in someone's code and I had to think for a sec. At
first I read it as converting a 'const char *' to a 'std::string *'

void f(const std::string &s)
{
std::cout << s.size() << "\n";
}

int main()
{
const char *c="sal";

f(c); //1
f(std::string(c)); //2
f(static_cast<std::string>(c)); //3

return 0;
}
If it works without a cast, you shouldn't use a cast. So (1) is probably the
best solution, not only because it's also the one with least typing. This
will also help you not to cast away errors, which may happen if you have to
change the type of the function's argument or the value passed to it latter.
In your example, that might be not likely to happen, but with other types,
like int's and long's it could cause problems.

Except for their syntax (2) and (3) do basically the same, so if you really
have to use a cast, use whichever looks better to you. There is less typing
with a function-style cast (2), but it is easier to find a static_cast in a
large program, especially with tools like grab. Also, a static_cast (or
other *_cast's) can be used for all types whereas function-style casts can
only be used for named types. For example, you can use static_cast<unsigned
int>(...) but you cannot use unsigned int(...) as a cast.

Regards
Heinz

Aug 9 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by selder21 | last post: by
13 posts views Thread by Gil | last post: by
9 posts views Thread by Mathieu Malaterre | last post: by
5 posts views Thread by Torben Laursen | last post: by
5 posts views Thread by Jae | last post: by
9 posts views Thread by priyanka | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.