When is a C++ destructor automatically invoked?
Correct answer
C. When the object lifetime ends, such as scope exit
15 views
C. When the object lifetime ends, such as scope exit
15 views
A destructor, written ~ClassName(), runs automatically when the object goes out of scope, is explicitly deleted, or is destroyed as a member of a dying container. Its job is to release resources the constructor acquired, from heap memory to file handles. For objects with automatic storage the compiler inserts the call, so a destructor should never be invoked manually on such an object.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is C? Ask here and get an explanation.