home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!usc!zaphod.mps.ohio-state.edu!cs.utexas.edu!sun-barr!lll-winken!taurus!taygeta.oc.nps.navy.mil!skip
- From: skip@taygeta.oc.nps.navy.mil (Skip Carter)
- Newsgroups: comp.lang.c++
- Subject: Re: Just what is so great about streams?
- Message-ID: <6994@taurus.cs.nps.navy.mil>
- Date: 16 Nov 92 21:32:29 GMT
- References: <1992Nov12.061942.16473@nuscc.nus.sg> <TMB.92Nov13102951@arolla.idiap.ch>
- Sender: news@taurus.cs.nps.navy.mil
- Reply-To: skip@taygeta.oc.nps.navy.mil (Skip Carter)
- Lines: 70
-
-
- 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 // writes a timestamp to a file
- {
-
- private:
-
- .....
-
- public:
- TimeStamp();
- ~TimeStamp() {}
-
- friend ostream& operator<<(ostream& os, const TimeStamp& t);
- };
-
-
- so I could write within an application (with an anonymous object),
-
-
- ofstream fout(....);
-
- fout << TimeStamp(); // writes a timestamp to the file attached to fout
-
-
- Now suppose I wrote a comment class for writing lines delimited by a user defined
- comment sequence:
-
- class Comment : public ostrstream // write a comment to a file, with user defined start and end
- { // comment sequences
- private:
-
- ....
-
- public:
- Comment(ostream& user_os, const char *cstart = "//",
- const char* cend = "\n");
- Comment(ostream& user_os, const char cstart,
- const char cend = '\n');
- ~Comment() {}
-
- ....
-
-
- };
-
-
- 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.
-
-
- ---
- Everett (Skip) Carter Phone: 408-646-3318 FAX: 408-646-2712
- Naval Postgraduate School INTERNET: skip@taygeta.oc.nps.navy.mil
- Dept. of Oceanography, Code OC/CR UUCP: ...!uunet!taygeta!skip
- Monterey, CA. 93943 TELEMAIL: s.carter/omnet
-