I consider changing how you stream based on the object you are streaming to do be a horrible idea that completely ignores the whole point of how the stream objects are intended to work. So, I would create a member class or function which returns an object of a type that handles the stream differently. So, for example, if you wanted to provide a colorized stream, you would call:
std::cout << myclass.colorstreamer << endl;
Edit:
Your proposal for handling streams is a bad idea because you have no clue how other people are going to use your code. It is completely unintuitive for a stream to behave differently depending on what object is doing the streaming. I liken this to having a function which returns a different result depending on who called it rather than dependent on what its arguments are, though I acknowledge that technically the stream is an argument.
As for how to do it this way, one way would be to create a colorstreamer
, make this new class a member of myclass
and make myclass
a member of colorstreamer
, then make colorstreamer
's stream operator a friend of myclass
. I'm more worried about the semantics of calling the function (i.e. using .colorstreamer
to control how it streams rather than using the stream itself) than I am about how to implement it. My suggestion for how to implement it is quite possibly a bad way to do it; my C++ is rusty.
<<
operator to do two different things depending on the stream, alternative approaches are welcomed – Johnettajohnette<<
to do two different things depending on the stream. Why do you want to do this? It seems like a really bad idea to me. – Festus