Figure 6 Implementing copy on write

class String {

// the contents of the string
// class from Figure 5.


...

// this indexing operator could
// modify the string
  char &operator[](int i)
  {
    if(imp->count > 1)
      Split();
    return imp->ptr[i];
  }
// This indexing operator won't
// modify, don't split.
  char operator[](int i) const
  {
    return imp->ptr[i];
  }
private:
  void Split()
  {
// Create private copy:
    imp->count--;
    imp = new
      StringImp(imp->ptr);
  }
};
// End of File