Re: C++ question

From: Jörgen Sigvardsson (di5sig@CSE.HKS.SE)
Date: 08/07/97


Kenneth G. Cavness wrote:

> >Hello All:
> >
> >In the source code, I know that there are lines similar to:
> >
> >    sprintf(buf, "Running game on port %d.", port);
> >    log(buf);
> >
> >I do understand that the purpose of this is to build a string in buf,
> and
> >then send it to the log function.  I'm currently working on porting
> the
> >code to C++, and was wondering if anyone new of the corresponding C++
>
> >equivalent to writing a stream to a buffer.  With the normal cout and
> cerr
> >commands, the string is written out to screen immediately, instead of
> being
> >stored in a buffer.
>
> Actually, it's just a string literal. Write yourself a string class,
> or use one of the ones provided all over the world. Then set your
> logging class or method to accept a string as a parameter. That's
> all the "buf" is -- a character array.
>

Actually I dont think that's what he wanted. I think he wants to
encapsulate the loging
mechanisms into an object. It could be done, pretty easily. Check this
out:

class Log {
public:
    Log(void) { /* insert object initialization code here*/ }
    Log& operator << (const char *); // In C++, you _should_ use a
string class here instead...(const String &)
};

Log& Log::operator << (const char *aLogmsg) {
    log(aLogmsg);  // This will log the string
    return *this;      // This will return the 'current' object as a
reference (notice the & at the return type)
                            // this allows stacking of << operators. Log
logObjM logObj << "1st msg to Log" << "2nd msg to Log"
}

Now the log-function has been encapsulated, and been given a LOT easier
interface. Of course you should add some
more operarors for integers, floats, bools and whatever so that Log can
output the correctly.

I hope that was the kind of answer you were looking for.

// Jorgen


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/08/00 PST