CSE687 - Object Oriented Design

Language Note #2
Resource Acquisition is Initialization (RAII)

Revised: 4/28/2011
Home Courses Code Handouts CoreTech Books Articles Math Research Masters Projects SWDev WebDev News OtherLinks SiteDesign Graduates AllPages Office Hours Notices

CSE681-SWMAA CSE686-IP CSE687-OOD CSE775-DO CSE776-DP CSE778-AWP CSE784-SWS

Cncp #01 Cncp #02 Cncp #03 Cncp #04 Cncp #05 Cncp #06 Cncp #07 Cncp #08 Cncp #09 Cncp #10 Cncp #11
Note #01 Note #02 Note #03 Note #04 Note #05 Note #06 Note #07 Note #08
Lang #01 Lang #02 Lang #03 Lang #04

Syllabus SG - Design SG - Templates SG - Class Relationships

Language Note #2:

The C++ language uses scope based deallocation. The destructor of any class instance is called when the thread of execution leaves the scope in which the instance was declared.

Conclusions for: Resource Acquisition is Initialization

Well designed value types created with C++ classes use scope based management of their resources.
  1. Any resources acquired by the class during its construction or invocation of other member functions is returned by its destructor.
  2. C++ conspires to automatically destroy all instances of types declared in any scope when the thread of execution leaves that scope.
  3. That means that the C++ language provides you the means to make your classes entirely responsible for managing their own resources. Users of the class don't have to know anything or remember anything to support that. They simply declare instances of the class in the scope where they need it and allocation and deallocation happen automatically.

    If users need more persistant life time, they get a pointer to an instance of your class on the heap by using the new operator; and when they have finished with the object, destroy it by calling delete on the pointer.