Which special member function is automatically called when a C++ object goes out of scope?

Options

  • A. Constructor
  • B. Destructor
  • C. Copy constructor
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

B. Destructor

Detailed Explanation

Answer: Destructor

A destructor is called when an object is destroyed, such as when a local object goes out of scope.

It is commonly used to release resources owned by an object.

For example, if a class manages a resource, its destructor can perform the required cleanup when the object lifetime ends.