ios_base& scientify(ios_base& in)
{
// make the flag modifications on in
return (in);
}
An additional improvement can be made; it is, however, not mandatory. As you expect that this will be heavily used, you may decide to make them inline functions in order to speed up your application. Also, note that the insertion and extraction operators are not defined for the "ios_base" class, so we need to use the internal function setf to apply the flags.
Inside the setf function, the flags can be combined with the bitwise or operator. Look over my example below:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
inline ios_base& scientify(ios_base& in)
{
in.setf( ios_base::showpos );
in.setf( ios_base::scientific | ios_base::showpoint);
return(in);
}
inline ios_base& resetFloat(ios_base& in)
{
in.setf( 0, ios_base::floatfield);
return(in);
}
int main( ) {
ios_base::fmtflags oldFlags = cout.flags( );
double i = 3.165;
cout << setprecision( 3);
cout << scientify << i << endl;
cout<< resetFloat << i << endl;
cout.flags(oldFlags);
}
+3.165e+000
+3.17