mardi 4 août 2015

Using temporary objects to do some tasks like log writing a good idea?

So I was searching for a good way to make a log manager to use in my programs, and I found an article, with a class similar to this(I simplified it to a few lines of code just to show it here) :

class Log
{
public:
   Log();
   virtual ~Log();
   std::ostringstream& Get();
protected:
   std::ostringstream os;
};
std::ostringstream& Log::Get()
{
      return os;
}
Log::~Log()
{
   //  Write the data from ostringstream;
}

The log record is written in the destructor, so to write a log record, you do this:

Log().Get() << "Log record";

It's a temporary object, so the log record would be written when this object is destroyed. Is it ok to do it like this ? Is this a better way than using a singleton ? Article with the class can be found here



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire