Bear::Save(std::ostream& out) {
out << "Fur is " << color << std::endl;
out << "Age is " << age << std::endl;
}
Editing somebody's code today, I needed an ostream equivalent of /dev/null, some stream into which a class could write without printing anything. This can be done by creating a stream buffer that never prints.
class nullbuf : public std::basic_streambuf
{
protected:
virtual int overflow(int c) { return c; }
};
class nullstream : public std::basic_ostream
nullbuf _streambuf;
public:
nullstream() :
std::basic_ostream
{ clear(); }
};
Using it looks like any other stream:
nullstream dev_null;
dev_null << "Nobody will ever see this.";
ostream* output = new nullstream();
(*output) << "This will be invisible.";
As usual, hope this helps.
No comments:
Post a Comment