By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,562 Members | 1,307 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,562 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
<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.