In a C++ program, we have 3 streams: stdin, stdout, and stderr. Can I override these in a console application and use them in a application that uses forms?
For example, if in some base class, I have cout<< "...", can I “redirect” to something visual (like a Windows Form)?
What I would recommend doing is having a class which wraps around an iostream like this :
Then, whenever you want to change where the data goes, you just change the class behavior.
Here is how you use the class:
[edit]
As potato swatter suggested, I’ll add an example with something other than cout:
As for why you should try this instead of inheriting from something like a string stream, mainly because you can easily change where the Logger outputs to dynamically. For instance, you could have three different output streams, and use a static member variable to swap between at runtime:
This can easily be expanded to create a powerful way of dealing with logging. You could add filestreams, use std::cerr, etc.