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