Singletons
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
// Very bad because the destruction order is not specified. | |
// From experience, these are one of the hardest to debug bugs. | |
template <class T> | |
T BadSingleton() { | |
static T t; | |
return t; | |
} | |
// Better. It is NOT a leak because the compiler will clear it after the shutdown. | |
// Downside -- destuctors are not called. But you should never do complicated | |
// things in destructors. | |
template <class T> | |
T BetterSingleton() { | |
static T* t = new T(); | |
return *t; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment