Figuring out mutators

Hi,

I'm hoping some folks can help me figure out mutators. Here is what I've learned thus far. I create a new int. I give that int a value. I pass that into to cout and it displays the ints value. Next I pass that int to hex and then cout. The result is that the value of my int is now displayed in hex form. Pretty cool. I decided to do a test, pass that int again to cout without using the hex mutator. Now that's interesting, its still displaying my int value in hex form. Hmm... my guess is that the mutator acts upon the int object? I've read the description of hex and dec on this sight but I can't at the moment wrap my head around what its doing. Also, I don't understand, if the mutators are acting upon the int object as I suspect, then why can't I use that mutator without cin or cout? I got an error C2296 when i tried.

Thanks,
D
hex is a manipulator not a mutator.

hex operates on the stream object (cout) not the integer. It sets a bit in the stream object that says all subsequent integers should be output in hex until you change it by using dec.
Interesting. Does that mean that the cout object persists between uses? I'm thinking a good test to show what your talking about would be to run two ints through cout in this mannor

cout << hex << int1
cout << int2

The expected result would be both ints showing in hex format yes?

Thanks much,
D
Yes both numbers will be in hex.
cout does keep it formatting options between statements. (There are some exceptions, setw() for example)
When in doubt, whip up a quick program to test it.
Last edited on
Topic archived. No new replies allowed.