P
pookiebearbottom
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;
}
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;
}