Under what conditions a destructor destroys an object?

- Scope of existence has finished
- Object dynamically assigned and it is released using the operator delete.
- Program terminated.
- Both a and b.

CORRECT ANSWER : Both a and b.

Discussion Board
C++ - Destructors

The above answer both a and b are right. Destructor is automatically invoked when the object is destroyed. It can happen when its lifetime is bound to scope and the execution leaves the scope, when it is embedded in another object which lifetime ends, or when it was allocated dynamically and is released explicitly. Its main purpose is to free the resources (memory allocations, open files or sockets, database connections, resource locks, etc.) which were acquired by the object during its life and/or deregister from other entities which may keep references to it.

Prajakta Pandit 01-24-2017 03:26 AM

A program could terminate in different ways.

I think the destructor is not necessary called when a programs terminates. It will depend on the way how it terminates, abort() or exit(). Then I think exit() actually calls all destructors but abort() does not.

Jean 04-7-2016 02:13 PM

C is also correct

The destructor is also called when the program terminates.

Vinicius 10-25-2015 07:40 PM

Write your comments


Enter the code shown above:
(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)