TITLE: output to multiple streams (Newsgroups: comp.lang.c++.moderated, 25 Sep 96) PROBLEM: steve@brecher.reno.nv.us (Steve Brecher) [wants to 'tee' streamed output] instead of cout << this << that << other; lout << this << that << other; I'd like to write something like my_dual_ostream << this << that << other; RESPONSE: herbs@cntc.com (Herb Sutter) [ I believe Herb is the author of "Learn C++ in 21 Days" or some title like that. -adc ] You might try the following (this is just a quick hack for demonstration purposes): /* op<< is global because my compiler doesn't support member templates, and the _targets list is public because my compiler also doesn't let me make the global op<< a friend */ class multiway_ostream { public: void add( ostream& os ) { _targets.push_back(&os); }; list _targets; }; template multiway_ostream& operator<<(multiway_ostream& mos, const T& t) { for( list::iterator i = mos._targets.begin(); i != mos._targets.end(); ++i ) { *(*i) << t; } return mos; } Now you can multicast to as many ostreams as you want: int main( int argc, char* argv[]) { ofstream of1( "test1.out" ); ofstream of2( "test2.out" ); multiway_ostream mos; mos.add( cout ); mos.add( of1 ); mos.add( of2 ); mos << "Hello" << " world" << endl; // goes to all three ostreams return 0; } Note that I deliberately didn't inherit from ostream because I didn't want a multiway_ostream passed around and used as an ostream. Without spending some time looking at ostream's definition, I'm not sure that all of the inherited state and functions would be appropriate. It's fine for this use, though.