Caffeinated Coder
Book an appointment
Lesson 14 · Classes

RAII: let objects clean up

Memory from new, an open file, a lock: each must be given back exactly once, and hand-written cleanup is easy to skip. RAII ties a resource to an object: the constructor takes it and the destructor gives it back, and C++ runs the destructor on every way out of a scope. Follow a small class Buf as it takes four ints from the heap and returns them on an early return and during an exception.

10 min9 steps5 questionsIntermediate
Fig 14 · Interactive
Loading figure…
or ← →

Quiz Unlocks after the last step.