Boost logo

Boost :

From: Dietmar Kuehl (dietmar_kuehl_at_[hidden])
Date: 2000-03-07 08:55:54


> I was aware that creating a new stream buffer was the "official" way
> - however, as all I was likely to do was to collect text into a
> string, which is precisely what a stringstream does, it seemed
> unreasonable to reimplement stringstream just for this...

Of course, you can use the important part of string streams, namely the
'std::stringbuf'. Here is a possible class for your debugging purposes:

  class odebugstream: public std::ostream {
    odebugstream(std::string const& t):
      std::ios(&m_sbuf), // virtual base of 'std::ostream'
    std::odebugstream& make_box() {
      ::MessageBox(m_title.c_str(), m_sbuf->str().c_str();

    std::string m_title;
    std::stringbuf m_sbuf;

  extern odebugstream dout;
  std::ostream& make_box(std:ostream& out) {
    return out;

This class would then be used via the special output stream 'dout':

  int main() {
    dout << "hello, world!" << make_box;

    dout << "goodbye";
> And using a manipulator is no good, as manipulators need to work
> on ostreams, not on derived classes... [Yes, I did think pretty hard
> about all this...]

It is not that unusual that manipulators try a dynamic_cast and act
depending on the result of this cast. The above stream does, for
example, throw a bad_cast if the stream to which 'make_box' is applied
is not a 'odebugstream'.

Do You Yahoo!?
Talk to your friends online with Yahoo! Messenger.

Boost list run by bdawes at, gregod at, cpdaniel at, john at