Is this the example of singleton pattern ?? If it is not then what can be the problems if we use this class as a logger. (Of-course it is not a full flexed logger )
#include <iostream>
#include <fstream>
using namespace std;
class logger
{
private:
static ofstream myfile;
void openfile()
{
myfile.open ("example.txt");
}
void closefile()
{
myfile.close();
}
public:
void logit(string str)
{
if (myfile.is_open() == 1)
{
myfile << str << endl;
}
else
{
openfile();
myfile << str << endl;
}
}
};
ofstream logger::myfile;
int main ()
{
logger l;
l.logit ("log from vod application");
logger l2;
l.logit ("log from guide application");
logger l3;
l1.logit ("log from application 3-1");
l1.logit ("log from application 3-2");
return 0;
}
Any discussion will be helpful.
Devesh