Path: utzoo!attcan!uunet!brunix!doorknob!jak From: jak@cs.brown.edu (Jak Kirman) Newsgroups: comp.lang.c++ Subject: Deriving new stream types: tstream Message-ID: Date: 31 Jan 91 14:54:44 GMT Sender: news@brunix.UUCP Reply-To: jak@cs.brown.edu Organization: Department of Computer Science, Brown University Lines: 47 I would like to create a type which behaves precisely as does an ostream except that the constructor would take two ostream references, and every operation would affect both ostreams equally. This would be very useful for debugging, where I often want a complete transcript of the debugging messages in a file, and also some of the debugging messages on the terminal (a run-time switch can make different outputs go to the file or to the tstream). If I spent a long time poring over the manuals, I could probably figure out how to do this right, but I thought I would first ask if anyone else has done this, or anything similar enough to provide either a cookbook approach or some hints on what to avoid or worry about. I managed to get something similar working by deriving a type tfilebuf from filebuf, and a type tstreambase from ios, containing a tstreambuf, then to derive tstream from ostream and tstreambase. ios filebuf | | Tstreambase ostream Tfilebuf \ / Tstream However, this was aesthetically unpleasing, and less general than I would have liked. I also only redefined the overflow function; I am a little concerned that some of the other virtual functions might also get called at some point. Clearly, in order to get the functionality of an ostream, I need to inherit from ostream, but since an ostream has data, I can only contain one ostream; I now have an assymetry between the two ostreams which are being printed to: one is a base class and one is a member. Also, why don't all the output functions defined on ostream resolve to calling a single function, like "put"? That surely would have made inheriting from ostream much simpler. I confess to not understanding the workings of streams fully, and would appreciate a brief, higher level description of how to derive from them than is presented by the man pages. Thanks. Jak jak@cs.brown.edu ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ I wouldn't recommend sex, drugs or insanity for everyone, but they've always worked for me. -- Hunter S. Thompson