Que*_*ueg 2 c++ io std ostream
这是一个示例C++代码
ostream& log = cout;
ostream& getLog() { return log; }
// somewhere in code
getLog() << "Message";
Run Code Online (Sandbox Code Playgroud)
执行此代码时,将打印"消息".
问:丢弃这些消息的最简单方法是什么(不打印,不保存)?getLog仍然必须由客户端使用,但可能会返回一些吞下所有消息的模拟输出流...
你可以做以下恐怖:
ostream nullstream(0);
ostream& log = nullstream;
Run Code Online (Sandbox Code Playgroud)
将null传递给构造函数ostream会设置badbit标志,因此会丢弃所有写入.