home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / IDIOMS.ZIP / 3-7.C < prev    next >
C/C++ Source or Header  |  1991-12-04  |  885b  |  38 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. #include "3-6.h"
  7.  
  8. class String {
  9. public:
  10.     String() {
  11.         rep = new StringRep; rep->count=1;
  12.     }
  13.     String(const String& s) {
  14.         rep = s.rep; rep->count++;
  15.     }
  16.     String& operator=(const String& s) {
  17.        s.rep->count++;
  18.        if (--rep->count <= 0) delete rep;
  19.        rep = s.rep;  return *this;
  20.     }
  21.     ~String()           {
  22.         if (--rep->count <= 0) delete rep;
  23.     }
  24.     String(const char *s) {
  25.        rep = new StringRep(s);
  26.        rep->count = 1;
  27.     }
  28.     String operator+(const String& s) const {
  29.        StringRep y = *rep + *s.rep;
  30.        return String(y.rep);
  31.     }
  32.     int length() const {
  33.         return rep->length();
  34.     }
  35. private:
  36.     StringRep *rep;
  37. };
  38.