Localized Ownership: Managing Dynamic Objects in C++


By: T. Cargill
Published in: PLoPD2
Pages: 5-18
Category: C++ Idioms

Summary: Management of dynamic object lifetimes in C++.

Pattern: Creator as Sole Owner

Pages: 8-12

When a dynamic object is created in C++, the creator can determine the object's lifetime. If there is a narrow purpose for a dynamic object, let the creator control its lifetime. A narrow purpose does not imply that the object is short-lived or that the creator has exclusive access to it.

Pattern: Sequence of Owners

Pages: 12-13

You're creating a dynamic object in C++ and can't use Creator as Sole Owner. Transfer ownership from the creator to another owner. Ownership may be transferred any number of times. This transfer must be an explicit part of the interface between the two. At any time, ownership rests with only one owner.

Pattern: Shared Ownership

Pages: 14-16

You're creating a dynamic object in C++ and can't use Creator as Sole Owner or Sequence of Owners. An object that defies simple characterization and management of its lifetime is likely to be long-lived. Allow arbitrary owners to declare unilateral interest in the dynamic object. The lifetime of the object continues until all the owners have relinquished their interest in it.