Interview Question

What is RAII?

RAII makes object lifetime control resource cleanup.

💡 Concept ✅ Quick Revision 🧠 C++

Answer

RAII binds a resource’s lifetime to the lifetime of an object. • A constructor establishes the object invariant and acquires or receives the resource. • The destructor releases the resource during normal scope exit and stack unwinding. • Standard types such as containers, locks, and smart pointers use this pattern.

💡 C++ Example

#include <fstream> void write_report() { std::ofstream file{"report.txt"}; file << "ready\n"; } // file closes automatically

⚡ Quick Revision

RAII makes object lifetime control resource cleanup.