To overload the operator<<
, the type needs to be different.
So, to do this, you will have to make a new class to replace std::ostream
, e.g. owarnstream
and oerrstream
.
I think something like this would work:
class oerrstream
{
private:
std::ostream& st;
public:
oerrstream(std::ostream &stream) : st(stream) {}
std::ostream& getStream() { return st; };
};
Then you could override it using:
oerrstream& operator<<(oerrstream &es, const std::string& s)
{
es.getStream() << s;
return es;
}
Just bear in mind that you will need to override ALL output operatins... It may work to do that using a template, like this:
template <typename T>
oerrstream& operator<<(oerrstream &es, T t)
{
es.getStream() << t;
return es;
}