home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / IDIOMS.ZIP / 3-11.H < prev    next >
C/C++ Source or Header  |  1991-12-04  |  873b  |  30 lines

  1. /* Copyright (c) 1992 by AT&T Bell Laboratories. */
  2. /* Advanced C++ Programming Styles and Idioms */
  3. /* James O. Coplien */
  4. /* All rights reserved. */
  5.  
  6. class String {
  7. friend class StringRep;
  8. public:
  9.     String operator+(const String &s) const { return *p + s; }
  10.     StringRep* operator->() const   { return p; }
  11.     String() {
  12.         (p = new StringRep())->count = 1;
  13.     }
  14.     String(const String& s)  { (p = s.p)->count++; }
  15.     String(const char *s) {
  16.         (p = new StringRep(s))->count = 1;
  17.     }
  18.     String operator=(const String& q) {
  19.         // a little different this time for variety's sake
  20.         if (--p->count <= 0 && p != q.p) delete p;
  21.         (p=q.p)->count++; return *this; 
  22.     }
  23.     ~String()       { if (--p->count<=0) delete p; }
  24. private:
  25.     String(char** r) {
  26.         p = new StringRep(r);
  27.     }
  28.     StringRep *p;
  29. };
  30.