home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / IDIOMS.ZIP / 3-14.H < prev    next >
C/C++ Source or Header  |  1991-12-04  |  816b  |  31 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 <stdlib.h>
  7. #include <string.h>
  8.  
  9. // a dumb string (not a concrete data type)
  10. // to demonstrate new/delete
  11.  
  12. class String {
  13. public:
  14.           String() { rep = new char[1]; *rep = '\e0'; }
  15.           String(const char *s) {
  16.               rep = new char[::strlen(s)+1];
  17.               ::strcpy(rep,s);
  18.           }
  19.           ~String() { delete[] rep; }
  20.    void*  operator new(size_t);
  21.    void   operator delete(void*);
  22.           // . . . .    // other interesting operations
  23. private:
  24.           static String *newlist;
  25.           union {
  26.                String *freepointer;
  27.                char *rep;
  28.           };
  29.           int len;
  30. };
  31.