This is all fine; I applied three manipulators with the creation of a new one, or transformed the reset of the float flags into a manipulator -- but even with this, I had to call the setprecision explicitly to set the precision. In a perfect scenario, I could do this with just the scientify(3) call.
To figure out how we can do this, let us investigate how to make this the developing team inside the STL library. How can the example on the previous page work? It works because inside the iostream library there is, defined for the insertion operator, the following:
basic_ostream& operator<<
(basic_ostream& (__clrcall *_Pfn)( basic_ostream&));
//where-> event_callback _Pfn; // pointer to event handler
Inside this, the function just takes a basic_ostream reference and returns the same reference; it simply takes a function and passes it further. Nevertheless, if you plan to pass some parameters inside the function as well, there is a problem. How can you pass them toward the function so that you will know the stream has them also? The obvious answer is to include the stream into its parameters.
Now if I start doing so I end up with something like this:
cin << streamify(3, cin);
This is a redundant solution and not elegant under any circumstances; we can find a better solution. The main power of the manipulators is to add it with the << operator to the function. We must not break that convention!
The solution is to trick the compiler. In addition, for this we need to use all of the overloading capabilities of C++. First, create a class that will do the work. For this we declare a local variable holding the input number and an overload for the () operator. The basic constructor will naturally exist inside, where we need to initialize the data.
class tempForScience
{
tempForScience (int n) { m_v = n };
void operator() (ostream& stream) const
{stream << scientify << setprecision(m_v); }; // here we can // whatever we want on the stream, for instance apply more // manipulators on it
int m_v;
}
This class will do the work for us, but we must initialize a type like this during the stream insertion, so we write a function that will just return a creation from this:
tempForScience setScientify(int n)
{
return tempForScience(n);
}
Now we called the function we want, but the problem that we still have is that the insertion operator will not know what to do with the tempForScience() object we just returned. We just need to take another step to resolve this issue.
ostream& operator<<(ostream& str, const tempForScience & tFS) {
tFS (str); // Pass the stream to the tfs object
// to do iterate back and accomplish all
return(str); // return the stream so we can insert // objects further in the stream
}
Insert all we learned for now after the scientify manipulator in the earlier code and change the call lines for the cout to the following:
cout << setScientify(3) << i << endl;
cout << resetFloat << i << endl;
The result is what we were aiming for, hurray!
+3.165e+000
+3.17