home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / cplus / 16500 < prev    next >
Encoding:
Text File  |  1992-11-18  |  2.4 KB  |  82 lines

  1. Path: sparky!uunet!usc!zaphod.mps.ohio-state.edu!cs.utexas.edu!sun-barr!lll-winken!taurus!taygeta.oc.nps.navy.mil!skip
  2. From: skip@taygeta.oc.nps.navy.mil (Skip Carter)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Just what is so great about streams?
  5. Message-ID: <6994@taurus.cs.nps.navy.mil>
  6. Date: 16 Nov 92 21:32:29 GMT
  7. References: <1992Nov12.061942.16473@nuscc.nus.sg> <TMB.92Nov13102951@arolla.idiap.ch>
  8. Sender: news@taurus.cs.nps.navy.mil
  9. Reply-To: skip@taygeta.oc.nps.navy.mil (Skip Carter)
  10. Lines: 70
  11.  
  12.  
  13.     In this thread about why one should WANT to use streams,
  14.     there has been a lot said about type-safety, runtime efficiency,
  15.     and extensibility with regard to I/O for new types.  However I have
  16.     yet to see anyone mention this advantage:   polymorphism with regard
  17.     to the I/O stream.
  18.  
  19.     Consider the following,
  20.  
  21. class TimeStamp                     // writes a timestamp to a file
  22. {
  23.      
  24.         private:
  25.  
  26.                .....
  27.  
  28.         public:
  29.           TimeStamp();
  30.          ~TimeStamp() {}
  31.  
  32.          friend ostream& operator<<(ostream& os, const TimeStamp& t);
  33. };
  34.  
  35.  
  36.              so I could write within an application (with an anonymous object),
  37.  
  38.  
  39.     ofstream fout(....);
  40.  
  41.     fout << TimeStamp();                           // writes a timestamp to the file attached to fout
  42.  
  43.  
  44.     Now suppose I wrote a comment class for writing lines delimited by a user defined
  45.     comment sequence:
  46.  
  47. class Comment : public ostrstream                   // write a comment to a file, with user defined start and end
  48. {                                                                             //  comment sequences
  49.         private:
  50.  
  51.                      ....
  52.                             
  53.         public:
  54.           Comment(ostream& user_os, const char *cstart = "//",
  55.                 const char* cend = "\n");
  56.           Comment(ostream& user_os, const char cstart,
  57.                 const char cend = '\n');
  58.          ~Comment() {}
  59.  
  60.              ....
  61.  
  62.  
  63. };
  64.  
  65.  
  66.     Now the TimeStamp class can write a comment WITH NO CHANGES MADE TO
  67.     THE TIMESTAMP CLASS.
  68.  
  69.     Comment fout(...);
  70.  
  71.     fout << TimeStamp();
  72.  
  73.  
  74.     In fact, TimeStamp can properly write to any class that is ultimately derived from ostream.
  75.  
  76.  
  77. --- 
  78.  Everett (Skip) Carter             Phone:  408-646-3318 FAX: 408-646-2712
  79.  Naval Postgraduate School         INTERNET: skip@taygeta.oc.nps.navy.mil
  80.  Dept. of Oceanography, Code OC/CR  UUCP:     ...!uunet!taygeta!skip
  81.  Monterey, CA. 93943               TELEMAIL: s.carter/omnet
  82.