Code snippet to give you the idea
filter_msg_buffer::filter_msg_buffer(const int msglen)
original_streambuf = cout.rdbuf ( (streambuf *) this);
// in the destructor, we restore the original streambuf for cout.
filter_msg_buffer::~filter_msg_buffer()
cout.rdbuf ( original_streambuf );
The streambuf constructor retains the pointer to cout’s old streambuf and registers itself to be the new streambuf.
In the destuctor we reverse the process and restore the original streambuf.