Item 40. RAII

The C++ community has a long and proud tradition of inscrutable abbreviations and odd names for techniques. RAII manages to attain both oddness and inscrutability. RAII stands for “resource acquisition is initialization.” (No, not “initialization is resource acquisition,” as some would have it. If you’re going to be odd, you’ve got to go all the way or the whole thing falls flat.)

RAII is a simple technique that harnesses C++’s notion of object lifetime to control program resources like memory, file handles, network connections, audit trails, or whatever. The basic technique is simple. If you want to keep track of an important resource, create an object and associate the resource’s lifetime with the object’s lifetime. In that ...

Get C++ Common Knowledge: Essential Intermediate Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.