Skip to content

Instantly share code, notes, and snippets.

@kvanbere
Created June 13, 2013 13:59
Show Gist options
  • Save kvanbere/5773891 to your computer and use it in GitHub Desktop.
Save kvanbere/5773891 to your computer and use it in GitHub Desktop.
A singleton implementation which uses static singletons so you only need to use get<type>()->...
#include <iostream>
template <typename T>
class singleton
{
private:
static T* instance;
public:
static T* get()
{
return (instance ? instance : (instance = new T()));
}
};
template <typename T> T* singleton<T>::instance = 0;
template <typename T>
T* get()
{
return singleton<T>::get();
}
class one
{
public:
one()
{
std::cout << "one" << std::endl;
}
};
class two
{
public:
two()
{
std::cout << "two" << std::endl;
}
};
int main(int argc, char* argv[])
{
get<one>();
get<two>();
get<one>();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment