1

With the help of google I made a singleton logging class which is:

class Log{
public:
    void Initialize(const char* fileName, int logLevel, ...);
    void outString(const char* str, ...);
    void outError(const char* str, ...);
    void outWarning(const char* str, ...);
    static Log* GetInstance() 
    {
        if (!m_instance)
            m_instance = new Log();
        return m_instance;
    }
private:
    Log() {}
    Log(const Log&);
    Log& operator=(const Log&); 
private:
    static Log *m_instance;
    void SetColor(bool stdout_stream, Color color);
    string getCurrentTime();
    void ResetColor(bool stdout_stream);
    int m_logLevel;
    ofstream *m_file;
};

Now I want to know what the * is here : static Log *m_instance; Why do we set it as a pointer ? I don't really understand. I mean, what will it point to ?

Maxence Henneron
  • 495
  • 1
  • 4
  • 10

2 Answers2

2

It's pointer to the only one instance of your class .

You can access to this instance by 'Log::getInstance()' static function.

We use singleton pattern when we in practice don't need more than one instance of a class in our code.

Emadpres
  • 3,466
  • 2
  • 29
  • 44
2

It's a pointer so that it can initially be null; and then point to the instance when that's created using new on the first access; see the GetInstance function.

This is the "lazy leaky" variant of the Singleton anti-pattern: the instance is created when it's first accessed, and never destroyed. Like all attempts to implement a singleton in C++, it has some good points:

  • the instance is guaranteed to exist whenever it's accessed;
  • the instance doesn't take up any memory (except the pointer) if it's never accessed

and some bad points:

  • the instance is never destroyed, so may be reported as a memory leak;
  • the (probably minor) cost of checking the pointer on each access;
  • creation is not thread-safe, and it's non-trivial to make it thread-safe without incurring (possibly major) costs on each access.
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • I believe your answer is pretty complete, but apparently the OP has issues grasping the concept of pointers. Perhaps you could add a bit of guidance on that. – Lugaid Aug 29 '13 at 15:42
  • @Lugaid: If that's the case, then a [good introductory book](http://stackoverflow.com/questions/388242) would be more helpful then my ramblings. – Mike Seymour Aug 29 '13 at 15:44
  • So would that be dangerous to use this in multiple threads if I initialized the instance before I made the threads ? So now, why is there a pointer at my function getInstance and not just static Log GetInstance() ? – Maxence Henneron Aug 29 '13 at 15:49
  • 2
    @MaxenceHenneron: Initialising it before launching threads would be fine, as long as the object itself is thread-safe. The danger is if more than one thread tries to initialise it. – Mike Seymour Aug 29 '13 at 15:51
  • 1
    @MaxenceHenneron: `GetInstance` has to return a pointer or a reference. It can't return a value, since that would be a different instance, not the singleton we're trying to access. – Mike Seymour Aug 29 '13 at 15:53