I'm using this website as a reference and am modifying some code to (hopefully) have it work the way I'd like it to.

I think I'm close, but since I have no experience with custom paramaterized output manipulators (and have limited experience with custom output manipulators even) I'm kind of caught up.

My functions, with how I believe is the way I'd like to have the parameters issued, is:

First, within the class's header that will be using these manipulators I'd define these:

typedef map<ostream *, string> FlagMap;
static FlagMap Flags;

Then, outside of the class but within the header the following will be there:

struct yearManip {};
extern void year(yearManip x);
typedef void(*yearPtr)(yearManip);
extern ostream &operator << (ostream &out, yearPtr);[/code]

In the cc file, I'd have this:

Calendar::FlagMap Calendar::Flags;

void year(yearManip x) {}
ostream &operator << (ostream &out, yearPtr) {
    Calendar::Flags[&out] = "year";
    return out;
}

The above compiles happily, even when given an int as a parameter to the manipulator.

My problem is that I don't know how to retrieve the value of the int that's entered as a parameter for the manipulator. In the case above, my program will (as if on a calendar) display the one full calendar year that is entered as a number within the parameter).
Example of intended code usage:

Calendar mine;
cout << year(2010) << mine << endl;

The above would output the full calendar year for the year 2010.

I have all of the functions working correctly, already, but am having trouble understanding how to retrieve the parameter value so I can use the functions in this manipulator.

Can someone please help me understand how to retrieve that value so I can use it? It wouldn't be used by the user in any way...or in the final program. Just within the "Calendar" class's header and source file

Thanks in advance
-Josh

EDIT:
For example, this is the code I specifically need help with:

void year(yearManip x) {}
ostream &operator << (ostream &out, yearPtr) {
    Calendar::Flags[&out] = "year";
    Calendar::FlagsInt[&out] = yearPtr->x;
    return out;
}

(I decided to create a second FlagMap to store the ints relative to the ostream...so the same ostream can reference both the string and int)

I can't getCalendar::FlagsInt[&out] = yearPtr->x; to reference x, or Calendar::FlagsInt[&out] = yearManip.x; or any other variation I've tried so far

Recommended Answers

All 5 Replies

Ah, in the first post - the code that didn't show as code (must have missed a tag) should be:

struct weekManip { weekManip(int x); };
extern void week(weekManip x);
typedef void(*weekPtr)(weekManip);
extern ostream &operator << (ostream &out, weekPtr);

(I was also missing some code when I pasted it)

>Okay, while continuing to search, I came up with this:
>http://msdn.microsoft.com/en-us/libr...=VS.80%29.aspx

_Smanip is a Microsoft-ism. Don't expect code using it to compile anywhere except Visual C++. The concept behind _Smanip is trivial anyway:

#include <iostream>
#include <iomanip>

struct fillblank {
  int _l;

  fillblank ( int l ): _l ( l ) {}

  friend std::ostream& operator<< ( std::ostream& os, fillblank fb )
  {
    for ( int i = 0; i < fb._l; i++ )
      os << ' ';

    return os;
  }
};

int main()
{
  std::cout << "10 blanks follow" << fillblank ( 10 ) << ".\n";
}

The arguments are actually constructor arguments, which are then stored as data members in a temporary object and an overloaded << operator is called on the stream using that temporary object. It's elegant and works very well.

>Okay, while continuing to search, I came up with this:
>http://msdn.microsoft.com/en-us/libr...=VS.80%29.aspx

_Smanip is a Microsoft-ism. Don't expect code using it to compile anywhere except Visual C++. The concept behind _Smanip is trivial anyway:

#include <iostream>
#include <iomanip>

struct fillblank {
  int _l;

  fillblank ( int l ): _l ( l ) {}

  friend std::ostream& operator<< ( std::ostream& os, fillblank fb )
  {
    for ( int i = 0; i < fb._l; i++ )
      os << ' ';

    return os;
  }
};

int main()
{
  std::cout << "10 blanks follow" << fillblank ( 10 ) << ".\n";
}

The arguments are actually constructor arguments, which are then stored as data members in a temporary object and an overloaded << operator is called on the stream using that temporary object. It's elegant and works very well.

Thank you very much! I literally just found this link to a book that explained it this way and was working it out myself also. My example is very simple (simply re-displaying the int that is passed as an argument but it did show this works well.

The code's nice and short, and pretty easy to understand overall. Though, that was a lot harder to find than I had anticipated..I've been searching on-and-off for a couple days online and pretty intensely for the last few hours today

Thanks a lot for the help :)

I'll play with it a bit more to get it to do exactly what I'm looking for. I really appreciate the help!

This is really nice. Used in conjunction with the ability to have as many different manipulators as you'd like and choosing from an if-else which to execute, this is very powerful

Definitely something I'm going to implement in much of my future code I think

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.