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 ?

2
  • 2
    look 8 lines above the declaration of this mysterious *... Commented Aug 29, 2013 at 15:00
  • Please note this singleton isn't thread safe, and if that's important to you then you'll to change things a bit. Commented Aug 29, 2013 at 15:12

2 Answers 2

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.
Sign up to request clarification or add additional context in comments.

5 Comments

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: If that's the case, then a good introductory book would be more helpful then my ramblings.
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() ?
@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.
@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.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.