Double check code
Aug 31, 2016 at 8:29am UTC
is there any problem with this, what's wrong?
string* greeting() {
string s(“Hi!”);
return &s;
}
Is it that string doesn't have type?
Aug 31, 2016 at 8:48am UTC
Why are you returning a pointer? s goes out of scope at the end of the function and is destroyed. Also it's generally better to use std::string, rather than just string.
Aug 31, 2016 at 9:09am UTC
How would it be work tho?
Aug 31, 2016 at 9:30am UTC
Or just
1 2 3 4
std::string greeting()
{
return std::string("Hi!" );
}
However this seems like a pointless function, so without knowing why are doing this or in what context it's being used, it's difficult to help more.
Aug 31, 2016 at 9:57am UTC
why return a pointer and not a reference to?
1 2 3 4 5
string& greeting()
{
static string s(“Hi!”);
return s;
}
Topic archived. No new replies allowed.