Boltar wrote:
How persistant is the string created by substr()? Is it deleted
immediately the context exits or is it stored internally by the parent
string? I ask because I'm wondering if its safe to keep a copy of the
pointer returned doing mystr.substr(...).c_str() for a short while to
do some processing on it. Obviously if the sub string is deleted it'll
become invalid.
Like any temporary, the string returned by 'substr' lives as long as
the Standard defines. If no reference to const is bound to it, the
temporary lives till the end of the full expression that caused its
creation. If it's used to initialise an object, it lives until the
object is fully initialised. If a reference to const is bound to it,
it lives as long as the reference. The pointer you obtain using the
'c_str()' member function will be valid as long as the temporary is
alive (and unchanged).
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask