Quantcast
Channel: How do you implement the Singleton design pattern? - Stack Overflow
Viewing all articles
Browse latest Browse all 25

Answer by Yuriy for How do you implement the Singleton design pattern?

$
0
0

I did not find a CRTP implementation among the answers, so here it is:

template<typename HeirT>class Singleton{public:    Singleton() = delete;    Singleton(const Singleton &) = delete;    Singleton &operator=(const Singleton &) = delete;    static HeirT &instance()    {        static HeirT instance;        return instance;    }};

To use just inherit your class from this, like: class Test : public Singleton<Test>


Viewing all articles
Browse latest Browse all 25

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>