Under normal conditions the destructor is called between the return
seen in the code and the last statement that computed something (including one that is in the return
). The compiler inserts something there. What you may not know is that the return
is a code that does some things:
- one of them is result in something, that is, it can execute an expression, in your case it is only take the value of a variable, and "send" to whom called, most likely in a slot reserved in an expression in the calling function, or even more commonly assigned to a variable, which is nevertheless a slot reserved.
- another is the back from code execution control to caller location, what we actually call return.
Between one and the other the compiler would place the call from the destructor of string.
Don’t see your code as something flat that a line is a thing being executed.
Having said all that, there is no need to call destructor in this case and so nothing will be inserted in this code. There is the insertion of a value copy code, because it is not to destroy anything, the object needs to stay alive because it will be used in the calling function. There’s a move
and not a delete
, So it copies the pointer to the object, and it keeps the object alive, and the caller can, or cannot, destroy the object. At some point it is necessary to destroy, when there is no reference to it.
I do not know if it is from Portugal and there is used different, in Brazil we use the term destrutor.
Maniero, the behavior of both object destructors and object pointers is the same?
– Samuel Ives