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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!utcsri!skule.ecf!torn!nott!bnrgate!bcrka451!bcrki65!sjm
  3. From: sjm@bcrki65.bnr.ca (Stuart MacMartin)
  4. Subject: Re: Just what is so great about streams?
  5. Message-ID: <1992Nov18.205139.12711@bcrka451.bnr.ca>
  6. Sender: 5E00 Corkstown News Server
  7. Organization: Bell-Northern Research Ltd., Ottawa, Canada
  8. References: <6994@taurus.cs.nps.navy.mil> <1992Nov18.134551.5782@bcrka451.bnr.ca> <1992Nov18.182420.1@vax1.bham.ac.uk>
  9. Date: Wed, 18 Nov 1992 20:51:39 GMT
  10. Lines: 48
  11.  
  12. In article <1992Nov18.182420.1@vax1.bham.ac.uk> mccauleyba@vax1.bham.ac.uk (Brian McCauley) writes:
  13. >In article <1992Nov18.134551.5782@bcrka451.bnr.ca>, sjm@bcrki65.bnr.ca (Stuart MacMartin) writes:
  14. >> In article <6994@taurus.cs.nps.navy.mil> skip@taygeta.oc.nps.navy.mil (Skip Carter) writes:
  15. >>>    Now the TimeStamp class can write a comment WITH NO CHANGES MADE TO
  16. >>>    THE TIMESTAMP CLASS.
  17. >    ...
  18. >>>    In fact, TimeStamp can properly write to any class that is ultimately
  19. >>>      derived from ostream.
  20. >>>
  21. >> 
  22. >> What about     fout << t << t << endl;   
  23. >> The wrong operator<<() will be called for the second output unless you provide
  24. >> the appropriate friend function for TimeStamp, no?
  25. >No.
  26. >
  27. >What wrong operator<<()? There only is one that can possibly be called:
  28. >  ostream& operator<< (ostream&,const TimeStamp&)
  29. >
  30. >Are you thinking that << associates right?
  31. >
  32. >fout << t << t << endl;
  33. >  is not...
  34. >operator<<(fout,operator<<(t,operator<<(t,endl)))                             
  35. >
  36. >  it is..
  37. >operator<<(operator<<(operator<<(fout,t),t),endl)                             
  38. >                      \-----v---------/
  39. >                  Returns a ref to `fout'
  40. >           \---------------v--------------/
  41. >                Returns a ref to `fout'
  42. >
  43. >the inner to calls to operator<< call the same operator.
  44. >OK so an ostream& is used to refer to `fout' but when we get down to
  45. >the nitty-gritty of stream io everything is virtual so this doesn't matter.
  46.  
  47. Perhaps I was thinking too general.  If the class of fout does not need to
  48. redefine operator<<(), everything is ok.  But if there is some processing
  49. that has to be done before the data is passed to the underlying stream, 
  50. doesn't the class of fout have to redefine operator<<()?  In which case
  51. my example does not work.  Because operator<<() is *not* virtual.  I don't
  52. think it is true that TimeStamp can properly write to *any* class that 
  53. is ultimately derived from ostream.  And if it can, I'd sure like to know how.
  54.  
  55. Stuart
  56. --
  57. : Stuart MacMartin                                    email: sjm@bnr.ca      :
  58. : Bell-Northern Research                              phone: (613) 763-5625  :
  59. : PO Box 3511, Stn C, Ottawa, K1Y-4H7, CANADA    Standard disclaimers apply. :
  60.