c++ - Lifetime of temporary objects -
i encountered following code (roughly):
struct stringbuffer { stringbuffer(const char* string) {strcpy(m_buffer, string);} const char* c_str() const {return m_buffer;} char m_buffer[128]; }; std::string foobar() { const char* buffer = stringbuffer("hello world").c_str(); return std::string(buffer); }
am correct in assuming after line:
const char* buffer = stringbuffer("hello world").c_str();
buffer
pointing pointer in deconstructed stringbuffer
object?
to answer question @ end, yes, buffer
stray pointer.
to answer more general question lifetime of temporary values, suggest read this reference says:
... temporaries destroyed last step in evaluating full-expression (lexically) contains point created...
which case means once assignment buffer
done, temporary object destructed.
Comments
Post a Comment