Suppose you define a manipulator, xstream
, that works with the ostream class. The manipulator will work with all classes derived from ostream. Further suppose you need manipulators that work only with xstream
. In this case, you must add an overloaded insertion operator that is not a member of ostream:
xstream& operator<< ( xstream& xs, xstream& (*_f)( xstream& ) ) {
(*_f)( xs );
return xs;
}
The manipulator code looks like this:
xstream& bold( xstream& xs ) {
return xs << '\033' << '[';
}
If the manipulator needs to access xstream
protected data member functions, you can declare the bold
function as a friend of the xstream
class.