Overloading Manipulators

To provide an inline formatting mechanism. The basic template for parameterless manipulators is shown in the listing below.

Listing: Basic parameterless manipulator
ostream &manip-name(ostream &stream)
{
   // coding
   return stream;
} 
Remarks

Use overloaded manipulators to provide specific and unique formatting methods relative to one class.

A reference to ostream. (Usually the this pointer.)

See Also

<iomanip> for manipulators with parameters

Listing: Example of overloaded manipulator usage:
#include <iostream>
using namespace std;
ostream &rJus(ostream &stream);

int main()
{
   cout << "align right " << rJus << "for column";
   return 0; 
}

ostream &rJus(ostream &stream)
{
   stream.width(30);
   stream.setf(ios::right);
   return stream;
}

Result:

  align right                               for column