Re: [C++] ostrstream vs. sprintf

From: Daniel A. Koepke (dkoepke@circlemud.org)
Date: 02/07/02


On Thu, 7 Feb 2002, Karl B Buchner wrote:

> ch < vict << "You give it to " <<  'M' << ".\r\n";

Why not just use a unary manipulator?  Then you could do:

  ch << "You give it to " << act::name(vict) << '.' << act::endl;

This is a very simple, but powerful idea.  You can define manipulators
act::subjective() (to return the subjective [he/she/it] personal pronoun
for a character), act::objective() (to return the objective [him/her/it]
personal pronoun for a character), and act::possessive() (to return the
possessive [his/hers/its] pronoun for a character), among others.

In fact, if you're smart about how you design your streams, you can
eliminate act() altogether.  To write to a room, you might do:

  ch.InRoom() << act::exclude(ch) << act::exclude(vict)
              << "This message is seen by everyone in the room, "
              << "except ch and vict." << act::endl;

The act::exclude() manipulators would probably be implemented by returning
either a null stream (so it just drops everything) or the proper output
stream.  This is a little longer than the corresponding act() call, but
more readable and powerful.

> Of course support for more than just char * and int could be
> added...

...or could just make it part of the IOStreams framework and get all of
this automatically.  That's a code-intensive topic, which bears more words
than I can spare here.  A good book on advanced C++ should cover this.

> [... snip stuff on conversion ...]

Yes, you can use std::stringstream for conversion.  This isn't always a
good idea, since it introduces some overhead, but, in our case, simpler is
better than most efficient.  The overhead it introduces is mainly in code
size ("template bloat").

-dak

--
   +---------------------------------------------------------------+
   | FAQ: http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html |
   | Archives: http://post.queensu.ca/listserv/wwwarch/circle.html |
   | Newbie List:  http://groups.yahoo.com/group/circle-newbies/   |
   +---------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 06/25/03 PDT