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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!pipex!warwick!bham!bhamvx!mccauleyba
  3. From: mccauleyba@vax1.bham.ac.uk (Brian McCauley)
  4. Subject: Re: Just what is so great about streams?
  5. Sender: usenet@rs6000.bham.ac.uk (USENET News Service)
  6. Message-ID: <1992Nov19.131702.1@vax1.bham.ac.uk>
  7. Date: Thu, 19 Nov 1992 13:17:02 GMT
  8. Lines: 38
  9. References: <6994@taurus.cs.nps.navy.mil> <1992Nov18.134551.5782@bcrka451.bnr.ca> <1992Nov18.182420.1@vax1.bham.ac.uk> <1992Nov18.205139.12711@bcrka451.bnr.ca>
  10. Organization: University of Birmingham
  11.  
  12. In article <1992Nov18.205139.12711@bcrka451.bnr.ca>, sjm@bcrki65.bnr.ca (Stuart MacMartin) writes:
  13. > In article <1992Nov18.182420.1@vax1.bham.ac.uk> mccauleyba@vax1.bham.ac.uk (Brian McCauley) writes:
  14.  ...
  15. >>OK so an ostream& is used to refer to `fout' but when we get down to
  16. >>the nitty-gritty of stream io everything is virtual so this doesn't matter.
  17. > Perhaps I was thinking too general.  If the class of fout does not need to
  18. > redefine operator<<(), everything is ok.  But if there is some processing
  19. > that has to be done before the data is passed to the underlying stream, 
  20. > doesn't the class of fout have to redefine operator<<()?  In which case
  21. > my example does not work.  Because operator<<() is *not* virtual.  I don't
  22. > think it is true that TimeStamp can properly write to *any* class that 
  23. > is ultimately derived from ostream.  And if it can, I'd sure like to know how.
  24.  
  25. Yes this is true. This is why classes derived from a stream class should
  26. _not_ redifine operator<<.  Different stream classes differ only in their
  27. IO end not their data formatting end.
  28.  
  29. For a way to work round this problem don't
  30. derive your class form a stream class but give it an ostream& member
  31. and pass though all define:
  32.  
  33. template <class T> inline
  34. PseudoStream& operator<<(PseudoStream& ps, const T& data) {
  35.   ps.RealStream << data;
  36. }
  37.  
  38. Now you can define PseudoStream::operator<<(const SpecificType&)
  39.  
  40. Note PseudoStream objects can only be used in << statements not in the
  41. other ways that streams can be used.  
  42. -- 
  43.     \\   ( )    No Bullshit!     |  Email: B.A.McCauley@bham.ac.uk   
  44.  .  _\\__[oo        from         |  Voice: +44 21 471 3789 (home)
  45. .__/  \\ /\@    /~)  /~[   /\/[  |    Fax: +44 21 625 2175 (work)
  46. .  l___\\      /~~) /~~[  /   [  |  Snail: 197 Harborne Lane,
  47.  # ll  l\\    ~~~~ ~   ~ ~    ~  |         Birmingham, B29 6SS, UK
  48. ###LL  LL\\   (Brian McCauley)   |   ICBM: 52.5N 1.9W
  49.