mardi 31 mars 2015

When is the destructor of the temporary called

I wanted to know when the destructor of a temporay is called for both C++03 and C++11


Suppose I have the following case



foo method()
{
foo f;
......
......
return foo;
}

void doSomething()
{
foo f = method();
....
}


Suppose I am using the flag -fno-elide-constructors since I would like to get a theoretical understanding of when the destructor of a temporary is called. So from the above code in C++03 when the method() is finished a copy of foo is made using its copy constructor. After that at the statement foo f = method() the copy constructor of foo is called again. In this case for C++03 when is the destructor of this tempoary (which was passed by method) called ? Is it called at the end of scope of doSomething() Now I would like to apply the same case to C++11 which involve the move semantics. In case of C++11 when method returns a copy of foo is made.Then when foo f = method() is called the move constructor of foo is called. So in case of C++11 when is the destructor of the temporary object returned from method() called ?


Aucun commentaire:

Enregistrer un commentaire