I want to put a text description of a class instance onto an ostream, as in
ostream << myInstance;
I know how to declare an ostream inserter;
ostream& operator<<(ostream&, myClass&);
I want to be able to put different levels of detail to the ostream. I could do this if I could define two or more ostream inserters, or pass an extra argument to the inserter, or pass a method to the inserter, or perhaps call a method that returns a stringstream (can I do that?)
Has anyone solved this problem?
You can add “flags” into the stream by actually encaspulating the stream itself.
And then, you define a new operator for verbose output:
Usage:
You’ll note that
Verboseis not tied to the class, at all, and can be reused accross classes easily enough.Note: if you want to, you could add an extra parameter to
Verboseto actually control the level of verbosity directly there.