I would like to discuss the nuances of implementation of well known Singleton design pattern. Here there are two implementations in C++:
http://www.codeproject.com/Articles/1921/Singleton-Pattern-its-implementation-with-C
and another one is this:
#ifndef __SINGLETON_HPP_
#define __SINGLETON_HPP_
template <class T>
class Singleton
{
public:
static T* Instance() {
if(!m_pInstance) m_pInstance = new T;
assert(m_pInstance !=NULL);
return m_pInstance;
}
protected:
Singleton();
~Singleton();
private:
Singleton(Singleton const&);
Singleton& operator=(Singleton const&);
static T* m_pInstance;
};
template <class T> T* Singleton<T>::m_pInstance=NULL;
#endif
If we compare this versions what advantages and disadvantages does they have and eventually, which version is preferred?