Standard no-op output stream

后端 未结 6 1829
余生分开走
余生分开走 2020-11-29 03:25

Is there a way to create an ostream instance which basically doesn\'t do anything ?

For example :

std::ostream dummyStream(...);
dummyStream <<         


        
6条回答
  •  孤城傲影
    2020-11-29 03:47

    The basic method voor new stream classes is:

    1. Derive a class from std::streambuf;
    2. Override the virtual functions in that class. This is where the real work is done. In your case, empty implementations should be good enough.
    3. Derive a class from std::ostream with one member, your streambuf class.
    4. The constructor of your streamclass should forward the pointer to that member to the base constructor of std::ostream.

    I'm afraid you won't get rid of the formatting step, though.

    Hopefully this gives you some pointers; I don't have the time to expand this into a full answer, sorry.

    Update: See john's answer for details.

提交回复
热议问题