home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!know!cass.ma02.bull.com!catfish.az05.bull.com!elm!aking
- From: aking@trees.az05.bull.com (Andy King)
- Newsgroups: comp.lang.c++
- Subject: Implementing a 'message' class
- Message-ID: <1992Nov18.163232.2524@catfish.az05.bull.com>
- Date: 18 Nov 92 16:32:32 GMT
- Sender: news@catfish.az05.bull.com (placeholder for future)
- Reply-To: aking@trees.az05.bull.com (Andy King)
- Organization: Bull HN Information Systems Inc., Phoenix Product Division
- Lines: 51
-
- In C, it is relatively simple to create a facility for displaying
- messages which contain arbitrary data and data types. For example,
- I usually define a library function with a prototype similar to:
-
- void display_message(char* format, ...);
-
- and then use variable argument processing to create a message
- string. This message can then be displayed to the user (in a
- Motif dialog or Windows MessageBox, for example) and also written
- to a log file.
-
- I'd like to do the equivalent using the '<<' operator in C++, but
- I can't figure out how to implement the operator function. I
- envision the use of this class as follows:
-
- display_class display("logfile"); // an instance of the message class
-
- display << "Error opening " << file_name << " Status: " << errno;
-
- The '<<' operator in 'display_class' would put this message into a
- dialog box and write it to "logfile". It would also display the
- dialog and wait for the user to click on the "OK" button.
-
- Obviously, I can derive the 'display_class' from 'ofstream', and
- send all messages to a file. But I need to 'capture' the stream
- and manipulate it before it goes anywhere. Another solution would be
- to use 'strstream' as the base class for 'display_class', and to
- implement a member function which does the actual dispatching of the
- message. For example, after every message, the program would call:
-
- display.out();
-
- which would take the most recent message and send it to the screen
- and to a log file. But this seems as though it shouldn't be
- necessary. Of course, I could overload 'operator<<' for every type,
- but this is laborious, and would preclude the use of an existing
- 'ostream' based '<<' for user-defined types.
-
- Can this be done? How is the 'endl' specifier implemented in the
- 'ostream' class? Would it be possible to implement a similar facility,
- requiring that the programmer terminate a message with a class-defined
- specifier, thus activating the output processing?
-
- I'd really appreciate any suggestions. I'll post a summary, if
- appropriate.
-
- Thanks,
-
- Andy
-
- aking@trees.az05.bull.com
-