How to create a manipulator that will put to sleep or type "pause" in the console?

84 Views Asked by At

I'm having trouble creating the pause manipulator for the code below. "m" is an object that prints the given numbers in morsea code. This works for me, but I don't know how to make a manipulator for it. Can you help me with this problem?

long x = 0x2A6B5B5A;
double y = 8.23786789;
m<< x << pause << y ;
1

There are 1 best solutions below

11
463035818_is_not_an_ai On

It is not quite clear what your code is supposed to do. However, all you need is this:

struct pause_t {};

std::ostream& operator<<(std::ostream& out, const pause_t& p) {
     // put code here
     return out;
};

Put the code that does whatever you like to happen in place of //put code here, then use it like this:

pause_t pause;
std::cout << 42 << pause << 42;

If m in your example is not a std::ostream then you just need to adjust above overload for << accordingly.

PS: Strictly speaking the above is not an io manipulator. io manipulators are typically implemented as functions that take the ostream as parameter and std::ostream has an overload of << for such functions. However, that distinction is probably not important in your case.