home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / cplus / 16518 < prev    next >
Encoding:
Internet Message Format  |  1992-11-18  |  2.5 KB

  1. Path: sparky!uunet!know!cass.ma02.bull.com!catfish.az05.bull.com!elm!aking
  2. From: aking@trees.az05.bull.com (Andy King)
  3. Newsgroups: comp.lang.c++
  4. Subject: Implementing a 'message' class
  5. Message-ID: <1992Nov18.163232.2524@catfish.az05.bull.com>
  6. Date: 18 Nov 92 16:32:32 GMT
  7. Sender: news@catfish.az05.bull.com (placeholder for future)
  8. Reply-To: aking@trees.az05.bull.com (Andy King)
  9. Organization: Bull HN Information Systems Inc., Phoenix Product Division
  10. Lines: 51
  11.  
  12. In C, it is relatively simple to create a facility for displaying
  13. messages which contain arbitrary data and data types.  For example,
  14. I usually define a library function with a prototype similar to:
  15.  
  16.     void display_message(char* format, ...);
  17.  
  18. and then use variable argument processing to create a message
  19. string.  This message can then be displayed to the user (in a
  20. Motif dialog or Windows MessageBox, for example) and also written
  21. to a log file.
  22.  
  23. I'd like to do the equivalent using the '<<' operator in C++, but
  24. I can't figure out how to implement the operator function.  I
  25. envision the use of this class as follows:
  26.  
  27.     display_class display("logfile"); // an instance of the message class
  28.  
  29.     display << "Error opening " << file_name << " Status: " << errno;
  30.  
  31. The '<<' operator in 'display_class' would put this message into a
  32. dialog box and write it to "logfile".  It would also display the
  33. dialog and wait for the user to click on the "OK" button.
  34.  
  35. Obviously, I can derive the 'display_class' from 'ofstream', and
  36. send all messages to a file.  But I need to 'capture' the stream
  37. and manipulate it before it goes anywhere.  Another solution would be
  38. to use 'strstream' as the base class for 'display_class', and to
  39. implement a member function which does the actual dispatching of the
  40. message.  For example, after every message, the program would call:
  41.  
  42.     display.out();
  43.  
  44. which would take the most recent message and send it to the screen
  45. and to a log file.  But this seems as though it shouldn't be
  46. necessary.  Of course, I could overload 'operator<<' for every type,
  47. but this is laborious, and would preclude the use of an existing
  48. 'ostream' based '<<' for user-defined types.
  49.  
  50. Can this be done?  How is the 'endl' specifier implemented in the
  51. 'ostream' class?  Would it be possible to implement a similar facility,
  52. requiring that the programmer terminate a message with a class-defined
  53. specifier, thus activating the output processing?
  54.  
  55. I'd really appreciate any suggestions.  I'll post a summary, if
  56. appropriate.
  57.  
  58. Thanks,
  59.  
  60. Andy
  61.  
  62. aking@trees.az05.bull.com
  63.