Skip to content

Instantly share code, notes, and snippets.

@ofZach
Created November 26, 2014 05:09
Show Gist options
  • Save ofZach/787439f86753b7c6a8c6 to your computer and use it in GitHub Desktop.
Save ofZach/787439f86753b7c6a8c6 to your computer and use it in GitHub Desktop.
singleton template
#include <stddef.h> // defines NULL
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;
@ofZach
Copy link
Author

ofZach commented Nov 29, 2014

usage:

typedef Singleton < classname >  singletonName;
singletonName::Instance()->...

ie:

class blah{
  public:  
  void helloWorld();
};

typedef Singleton < blah > blahSingleton;

blahSingleton::Instance()->helloWorld();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment