H
H
Hrefix2015-12-08 23:22:27
C++ / C#
Hrefix, 2015-12-08 23:22:27

When is a destructor implicitly called?

Question search did not return any answers. Google and books don't give a clear answer.
The question is rather vague for me. Please list the possible options (with the exception of calls to exit the scope of the block; plus, from the same opera, on return)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Oleg Tsilyurik, 2015-12-08
@Olej

Please list possible options.

There is only one option: the destructor in C++ is called exactly at the moment when the object is destroyed .
Everything. Dot.
There are no other "options" and there cannot be, because in C ++ there can be no garbage collection.
But when an object is destroyed, there may be some options: explicit and implicit destruction, destruction on exit from the block, destruction according to the program epilogue ... Here the difference is already in that. what is called the "lifetime" of an object.

A
Alexander Ruchkin, 2015-12-09
@VoidEx

Draft of the n3337 standard:
Destructors are invoked implicitly
- for constructed objects with static storage duration (3.7.1) at program termination (3.6.3),
- for constructed objects with thread storage duration (3.7.2) at thread exit,
- for constructed objects with automatic storage duration (3.7.3) when the block in which an object is created exits (6.7),
— for constructed temporary objects when the lifetime of a temporary object ends (12.2),
— for constructed objects allocated by a new- expression (5.3.4), through use of a delete-expression (5.3.5),
- in several situations due to the handling of exceptions (15.3).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question