Previous section   Next section

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


Chapter 4. Data Encapsulation and Value Types

In the last chapter we looked at resource encapsulation as distinct from data encapsulation. Where resource encapsulation is more about mechanism than meaning, data encapsulation can be said to be the opposite (though the distinctions do blur from case to case).

Data encapsulation provides the classic object-oriented encapsulation benefits:

  1. Coherence of data. Object instance state can be initialized to a meaningful whole, and subsequent manipulations of the instance via its interface methods are done atomically; the instance will have consistent members before a method is called and after the method call is complete.

  2. Reduction of complexity. Client code manipulates a straightforward public interface to the object instances, and does not know of, or care about, the level of internal complexity.

  3. Immunity to change. Client code is insulated from changes to the internal implementation of the type, as well as supporting generic techniques operating on several types with similar public interfaces but differing internal representations.

Classes that implement data encapsulation may also implement resource encapsulation—strings are a great example—but the resource encapsulation is the "how" and we're going to focus on the "what" here.

Further to the issue of data encapsulation is the notion of value types. In this chapter we're going to distinguish the difference between value types and entity types and examine in detail what it means to be a value type. We also examine how differing levels of encapsulation affect the definitions of value types.


      Previous section   Next section