Archived

This topic is now archived and is closed to further replies.

rick_appleton

CLog << "text" ???

Recommended Posts

I''m writing a logging routine, and I would love to just be able to do something like this:
CLog myLogger;
myLogger << "out put this text to logfile" << endl;
 
I''ve searched all over the net, but can''t find anything on doing this. All the tutorials I''ve found are about doing
cout << myLogger
 
but that is of no use to me. Can this be done or should I forget about overloading the << ?

Share this post


Link to post
Share on other sites
class CLog
{

public:

CLog& operator<<(const std::string& string)
{
std::cout << "hmmn, what should I do with this string of length " << string.length() << " that I've got?\n";
return *this;
}

};


Enigma

EDIT: change to same name as your class

[edited by - Enigma on January 25, 2004 11:58:22 AM]

Share this post


Link to post
Share on other sites
Sure, you can do that. Just define a suitable operator<<.


CLog& operator<<(CLog &log, const char *str)
{ log.Log(str); return log; }


Something like that. Note that it is not a member function
and it needs to call a member function of the CLog class
to do the actual logging.

Repeat for other data types (or go for a template if you wish).

[Edit]: Added a return.



Kami no Itte ga ore ni zettai naru!

[edited by - tangentz on January 25, 2004 12:20:09 PM]

Share this post


Link to post
Share on other sites