## 18.5 Example: Reference Counting

Consider our `String` class: change the semantics for the assignment of strings so that two strings would share a common internal data buffer:

- Structure holding reference count and the buffer is moved to a nested class, `StringReference`
- References are managed by `reassign`
  - Takes ptr to a `StringReference`
  - If not NULL, reference to argument value is incremented
  - Reference to current value is decremented
  - If current reference is 0, object is deleted
  - Current object is set to refer to argument value

prev |top |next