Created
June 13, 2013 13:59
-
-
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>()->...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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