In the following example, the macro IOMANIPdeclare is called with the user-defined class my_class as an argument. One of the classes that is produced, OMANIP(my_class), is used to define the manipulator pre_print().
#include <iostream.h> #include <iomanip.h>
class my_class {
public: char * s1;
const char c;
unsigned short ctr;
my_class(char *theme, const char suffix,
unsigned short times):
s1(theme), c(suffix), ctr(times) {};
};
// print a character an indicated number of times // followed by a string
ostream& produce_prefix(ostream& o, my_class mc) {
for (register int i=mc.ctr; i; --i) o << mc.c ;
o << mc.s1;
return o;
}
IOMANIPdeclare(my_class);
// define a manipulator for the class my_class
OMANIP(my_class) pre_print(my_class mc) {
return OMANIP(my_class) (produce_prefix,mc);
}
void main() {
my_class obj("Hello",'-',10);
cout << pre_print(obj) << "\0" << endl;
}
This example produces the following output:
----------Hello