Raider wrote:
What is returned objects life-time?
Since it's a temporary, refer to "the life time of a temporary"
in your favourite C++ book. Hint: any temporary *usually* lives
until the full expression where it's created is evaluated, except
when it lives longer if bound to a reference to const.
For example, is it safe:
#include <...>
std::string foo()
{
return std::string("foo");
}
std::string bar()
{
std::string bar("bar");
return bar;
}
void test(char* a, char* b)
{
...
}
int main()
{
test(foo().c_str(), bar().c_str());
That shouldn't compile. Your 'test' needs pointers to non-const char
and 'c_str' returns a pointer to const char.
}
Here I think might be a problem. foo() returns std::string objects,
c_str() returns some pointer into the std::string("foo") and passes
this pointer to the test(). But will std::string("foo") exist when
test() is executed?
From the lifetime point of view, yes. You still need to make your
code const-correct.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask