Jump to content

Resource acquisition is initialization

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by E=MC^2 (talk | contribs) at 01:08, 19 May 2005 (stub). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Resource Acquisition Is Initialization (often referred to by the acronym RAII) is a technique in object oriented programming.

The technique states that, like the name implies, objects should initialise and release all resources they acquire.

It is of paramount importance especially in languages that do not support built-in garbage collection, such as C++.

A stereotypical example of RAII is the Standard Template Library's file-stream class. An input file stream is opened in the object's constructor, and it is closed upon destruction of the object. Since C++ allows objects to be allocated on the stack, C++'s scoping mechanism can be used to control file access.