+ 8

How do you create a manipulator that can be used even inside setf? [ANSWERED]

I have a vector class and I want to provide a feature to allow the user to see the vector either in the terms of i, j and k, or display its magnitude and the angles with the coordinate axes. I wanted to do both of these using the ostream operator, so I decided to make a manipulator. I searched the net and made this: ostream& ijk_not(ostream& os); // In this I turn a variable true, thats all. Now I was able to do: cout<<ijk_not<<vec1; But now I want this to work: cout.setf(ijk_not); How to do this?

9th Dec 2017, 7:14 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
15 Respostas
+ 4
Note : I agree my 'so called manipulator' is not at all a manipulator. Its just an ostream function that tricks the class to do things my way. I opened the iomanip header's library and found that setf accepts a variable of type ios_base, and it performs a bitwise xor/or (I don't remember) on this flag and perhaps thats what switches it on. But I cannot declare an object of type ios_base. I need help to create a successful manipulator! Please help!
9th Dec 2017, 7:19 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 2
@Gordie I am sorry. I had an exam. Ill just post the class. Thank you very much for the solution!
15th Dec 2017, 10:37 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 2
@Gordie I actually renamed ijk_not to simple, and the declaration now looks like this: const ios_base::fmtflags simple = static_cast<ios_base::fmtflags>(0x0); // This flag is the else version.
15th Dec 2017, 11:06 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
@Gordie The fmtflag works as expected with setf. But when I do : cout<<simple<<v1; The program instead of setting the flag and then printing the data as per the format prints the flag as it is (the data). Thus the flag doesn't work for both the things at the same time. Either I could get it working with setf, or with cout. Isn't there a way to do both?
15th Dec 2017, 10:59 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
Here is the code. https://code.sololearn.com/cnOnsphbGJk1/#cpp I have added both the manipulator and the format flag, but t seems there is an error as the names are causing ambiguity. But I never saw this happen with boolalpha, fixed or any other flags like these.
15th Dec 2017, 1:51 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
I wished to know something. When I print the cout.flags() bitwise, I get a certain number. But when I perform the setf function and after checking the change reprint the bit pattern after some cout operations, I get a different pattern from the one I saw after setf. Why? Now, If this continue, and I decide to use the 5th bit for my use, how will I know whether the change in my bit was genuinely caused by setf or just a cout operation? And secondly, I can focus on a single bit for manipulation, right?
15th Dec 2017, 2:03 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
@Gordie Thanks a lot! You made my day!
15th Dec 2017, 5:11 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar