Previous section   Next section

Imperfect C++ Practical Solutions for Real-Life Programming
By Matthew Wilson
Table of Contents
Part One.  Fundamentals


Chapter 3. Resource Encapsulation

Along with abstraction and polymorphism, encapsulation is one of the main tenets of object-oriented programming. Resource encapsulation is a refinement of encapsulation, insofar as the encapsulated data are actually references to allocated resources, albeit that resource is a broad term. A resource can be other class instances, allocated memory, system objects, API/library states, and object states.

Data encapsulation is the protection of an encapsulating type's inner state to ensure consistency of that type, or to abstract the public interface to it. Resource encapsulation, however, is the wrapping up of a resource reference in order to provide a more robust interface to that resource and to help in managing it; in other words it is a means of protecting the resource itself. The difference is subtle, and there's a lot of overlap in the real world between the two, to be sure, but it's worth being mindful of the distinction. This chapter and the next, Chapter 4, Data Encapsulation and Value Types, will cover both types of encapsulation.

Though most modern languages do very well at data encapsulation, C++ stands head and shoulders above its cousins in the C-language family in its support for resource encapsulation by virtue of the twin mechanisms of construction and automatic, deterministic destruction. In this chapter we look at the details of resource encapsulation and examine the different levels of encapsulation afforded by the language.


      Previous section   Next section