c++ - When is the destructor of the temporary called -


i wanted know when destructor of temporay called both c++03 , c++11

suppose have following case

foo method() {    foo f;    ......    ......    return foo; }  void dosomething() {    foo f = method();    .... } 

suppose using flag -fno-elide-constructors since theoretical understanding of when destructor of temporary called. above code in c++03 when method() finished copy of foo made using copy constructor. after @ statement foo f = method() copy constructor of foo called again. in case c++03 when destructor of tempoary (which passed method) called ? called @ end of scope of dosomething() apply same case c++11 involve move semantics. in case of c++11 when method returns copy of foo made.then when foo f = method() called move constructor of foo called. so in case of c++11 when destructor of temporary object returned method() called ?

i wanted know when destructor of temporay called both c++03 , c++11

the destructor of r-value (temporary) called @ end of expression.

give code:

foo method() {    foo f;    ......    ......    return foo; }  void dosomething() {    foo f = method();    .... } 

method() creates object. when object goes out of scope (at end of method), destructor called (in event of no optimisations).

the call "foo f="... causes copy constructor foo called. after expression ends, causing returned object (temporary) destructed. object "f"'s destructor called when goes out of scope @ end of dosomething.


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -