home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_11_09 / 1109107a < prev    next >
Text File  |  1993-07-18  |  1KB  |  56 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. //
  5. // String class with overloaded cat function
  6. //
  7. class String
  8.     {
  9. public:
  10.     String() { ... }
  11.     String(const char *s) { ... }
  12.     String(const String &s) { ... }
  13.     ~String() { delete [] str; }
  14.         // the [] were missing
  15.     size_t length() { return len; }
  16.     const char *text() { return str; }
  17.     void cat(char c);
  18.     void cat(const char *s);
  19.     void cat(const String &s);
  20.     char &sub(size_t i) { ... }
  21. private:
  22.     size_t len;
  23.     char *str;
  24.     };
  25.  
  26. // ...
  27.  
  28. //
  29. // append a nul-terminated string to a String
  30. //
  31. void String::cat(const char *s)
  32.     {
  33.     size_t n = len + strlen(s) + 1;
  34.     char *p = strcpy(new char[n], str);
  35.     strcat(p, s);
  36.     delete [] str;  // the [] were missing
  37.     str = p;
  38.     len = n;        // this line was missing
  39.     }
  40.  
  41. //
  42. // append a String to a String
  43. //
  44. void String::cat(const String &s)
  45.     {
  46.     size_t n = len + s.len + 1;
  47.     char *p = strcpy(new char[n], str);
  48.     strcat(p, s.str);
  49.     delete [] str;  // the [] were missing
  50.     str = p;
  51.     len = n;        // this line was missing
  52.     }
  53.  
  54. // ...
  55.  
  56.