home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!wupost!sdd.hp.com!hp-cv!ogicse!willamette.edu!rring
- From: rring@willamette.edu (Robert Ring)
- Newsgroups: comp.lang.c++
- Subject: Format State Question
- Message-ID: <1992Jul22.163241.26675@willamette.edu>
- Date: 22 Jul 92 16:32:41 GMT
- Article-I.D.: willamet.1992Jul22.163241.26675
- Followup-To: rring@jupiter.willamette.edu
- Organization: Willamette University, Salem, OR
- Lines: 47
-
- Hello, world.
-
- I would like to be able to support the following code
- which would print out my object in two different
- formats:
-
- #include "foo.h"
- #include <iostream.h>
-
- main()
- {
- foo x;
- cin >> x;
- cout << x; // this line prints x out normally
- cout << hex << x; // this line prints x in hex
- return 0;
- }
-
- using the standard hex stream manipulator. I read
- Lippman and discovered setf(), which eventually led to
- the following code:
-
- ostream& operator<<(ostream& os, const foo& x)
- {
- long current_base = os.setf(ios::dec, ios::basefield);
- if (current_base & ios::dec)
- {
- // output foo in "normal" format
- }
- else
- {
- // output foo in "hex" format
- }
- os.setf(current_base, ios::basefield);
- return os;
- }
-
- This works, but it's clumsy.
-
- Question: Is there any way to directly check the format
- state of cout, that is, not go through the above gyrations
- with setf?
-
- Thanks in advance.
-
- --R.
- --
-