home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / cplus / 11328 < prev    next >
Encoding:
Text File  |  1992-07-22  |  1.3 KB  |  59 lines

  1. Path: sparky!uunet!wupost!sdd.hp.com!hp-cv!ogicse!willamette.edu!rring
  2. From: rring@willamette.edu (Robert Ring)
  3. Newsgroups: comp.lang.c++
  4. Subject: Format State Question
  5. Message-ID: <1992Jul22.163241.26675@willamette.edu>
  6. Date: 22 Jul 92 16:32:41 GMT
  7. Article-I.D.: willamet.1992Jul22.163241.26675
  8. Followup-To: rring@jupiter.willamette.edu
  9. Organization: Willamette University, Salem, OR
  10. Lines: 47
  11.  
  12. Hello, world.
  13.  
  14. I would like to be able to support the following code
  15. which would print out my object in two different
  16. formats:
  17.  
  18.   #include "foo.h"
  19.   #include <iostream.h>
  20.   
  21.   main()
  22.     {
  23.     foo x;
  24.     cin >> x;
  25.     cout << x;      // this line prints x out normally
  26.     cout << hex << x;     // this line prints x in hex
  27.     return 0;
  28.     }
  29.  
  30. using the standard hex stream manipulator.  I read
  31. Lippman and discovered setf(), which eventually led to
  32. the following code:
  33.  
  34. ostream& operator<<(ostream& os, const foo& x)
  35.   {
  36.   long current_base = os.setf(ios::dec, ios::basefield);
  37.   if (current_base & ios::dec)
  38.     {
  39.     // output foo in "normal" format
  40.     }
  41.   else
  42.     {
  43.     // output foo in "hex" format
  44.     }
  45.   os.setf(current_base, ios::basefield);
  46.   return os;
  47.   }
  48.  
  49. This works, but it's clumsy.
  50.  
  51. Question:  Is there any way to directly check the format
  52. state of cout, that is, not go through the above gyrations
  53. with setf?
  54.  
  55. Thanks in advance.
  56.  
  57. --R.
  58. -- 
  59.