home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / cplus / 16504 < prev    next >
Encoding:
Internet Message Format  |  1992-11-18  |  1.9 KB

  1. Path: sparky!uunet!olivea!charnel!rat!usc!zaphod.mps.ohio-state.edu!cs.utexas.edu!torn!nott!bnrgate!bcrka451!bcrki65!sjm
  2. From: sjm@bcrki65.bnr.ca (Stuart MacMartin)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Just what is so great about streams?
  5. Message-ID: <1992Nov18.134551.5782@bcrka451.bnr.ca>
  6. Date: 18 Nov 92 13:45:51 GMT
  7. References: <1992Nov12.061942.16473@nuscc.nus.sg> <TMB.92Nov13102951@arolla.idiap.ch> <6994@taurus.cs.nps.navy.mil>
  8. Sender: 5E00 Corkstown News Server
  9. Organization: Bell-Northern Research Ltd., Ottawa, Canada
  10. Lines: 39
  11.  
  12. In article <6994@taurus.cs.nps.navy.mil> skip@taygeta.oc.nps.navy.mil (Skip Carter) writes:
  13. >
  14. >    In this thread about why one should WANT to use streams,
  15. >    there has been a lot said about type-safety, runtime efficiency,
  16. >    and extensibility with regard to I/O for new types.  However I have
  17. >    yet to see anyone mention this advantage:   polymorphism with regard
  18. >    to the I/O stream.
  19. >
  20. >    Consider the following,
  21. >
  22.  
  23. [class TimeStamp]
  24. >         friend ostream& operator<<(ostream& os, const TimeStamp& t);
  25. >};
  26. >
  27. >class Comment : public ostrstream                   // write a comment to a file, with user defined start and end
  28. >{                                                                             //  comment sequences
  29. >
  30. >    Now the TimeStamp class can write a comment WITH NO CHANGES MADE TO
  31. >    THE TIMESTAMP CLASS.
  32. >
  33. >    Comment fout(...);
  34. >
  35. >    fout << TimeStamp();
  36. >
  37. >
  38. >    In fact, TimeStamp can properly write to any class that is ultimately derived from ostream.
  39. >
  40.  
  41. What about     fout << t << t << endl;   
  42. The wrong operator<<() will be called for the second output unless you provide
  43. the appropriate friend function for TimeStamp, no?
  44.  
  45. Stuart
  46.  
  47. --
  48. : Stuart MacMartin                                    email: sjm@bnr.ca      :
  49. : Bell-Northern Research                              phone: (613) 763-5625  :
  50. : PO Box 3511, Stn C, Ottawa, K1Y-4H7, CANADA    Standard disclaimers apply. :
  51.