S
Stefan Ram
I still struggle with C++ memory management, so here are some
questions:
There seem to be three fundamental lifetimes of objects in C++?
- scope-based lifetime (auto variables)
- dynamic lifetime (»new«)
- temporaries (expression based?)
When »a« and »b« are instances of »::std::string«, what is the
lifetime of »a+b«?
A tutorial page, chosen at random,
http://www.cprogramming.com/tutorial/string.html
does not seem to mention or answer this question. Shouldn't
this be very important for every programmer to know from the
moment he starts to use objects?
When a function returns a instance of a class, is it the most
common case that it returns a temporary, so that this can be
assumed, unless the documentation tells something else?
Is it ok to return a temporary, as in the following example?
::std::string f()
{ ::std::string const a( "alpha" );
::std::string const b( "beta" );
return a + b; }
If this is a temporary, IIRC, it is guaranteed to exist during
the evaluation of the full-expression of the call site? So one
either has to pass it to another function there or copy it to
an object with longer life time.
The tutorial also uses:
string my_string3 = my_string1 + my_string2;
This seems to be »copy initialization«.
Is it of any advantage to use »direct initialization« in this
case? Which would be:
string my_string3( my_string1 + my_string2 );
Are the any guidelines, when to prefer copy initialization
and when to prefer direct initialization?
Ok, and I assume, what one can never to with a temporary
object, is to bind a reference name to it, as in:
string & my_string3( my_string1 + my_string2 );
But, this would be allowed with a const reference, because
for this special case ISO C++ extends the lifetime?
string const & my_string3( my_string1 + my_string2 );
Is this what »GotW #88« is about?
So for the same reason, a function with a reference parameter
can not be called with a temporary, unless the reference
parameter is const?
questions:
There seem to be three fundamental lifetimes of objects in C++?
- scope-based lifetime (auto variables)
- dynamic lifetime (»new«)
- temporaries (expression based?)
When »a« and »b« are instances of »::std::string«, what is the
lifetime of »a+b«?
A tutorial page, chosen at random,
http://www.cprogramming.com/tutorial/string.html
does not seem to mention or answer this question. Shouldn't
this be very important for every programmer to know from the
moment he starts to use objects?
When a function returns a instance of a class, is it the most
common case that it returns a temporary, so that this can be
assumed, unless the documentation tells something else?
Is it ok to return a temporary, as in the following example?
::std::string f()
{ ::std::string const a( "alpha" );
::std::string const b( "beta" );
return a + b; }
If this is a temporary, IIRC, it is guaranteed to exist during
the evaluation of the full-expression of the call site? So one
either has to pass it to another function there or copy it to
an object with longer life time.
The tutorial also uses:
string my_string3 = my_string1 + my_string2;
This seems to be »copy initialization«.
Is it of any advantage to use »direct initialization« in this
case? Which would be:
string my_string3( my_string1 + my_string2 );
Are the any guidelines, when to prefer copy initialization
and when to prefer direct initialization?
Ok, and I assume, what one can never to with a temporary
object, is to bind a reference name to it, as in:
string & my_string3( my_string1 + my_string2 );
But, this would be allowed with a const reference, because
for this special case ISO C++ extends the lifetime?
string const & my_string3( my_string1 + my_string2 );
Is this what »GotW #88« is about?
So for the same reason, a function with a reference parameter
can not be called with a temporary, unless the reference
parameter is const?