Create the Singleton Template - C++ template

C++ examples for template:template class

Description

Create the Singleton Template

Demo Code

#include <cassert>
#include <iostream>

using namespace std;

template <typename T>
class Singleton// w w w.  j a  v  a2 s . com
{
private:
    static T* m_Instance;

public:
    Singleton(){
        assert(m_Instance == nullptr);
        m_Instance = static_cast<T*>(this);
    }

    virtual ~Singleton(){
        m_Instance = nullptr;
    }

    static T& GetSingleton(){
        return *m_Instance;
    }

    static T* GetSingletonPtr(){
        return m_Instance;
    }
};

template <typename T>
T* Singleton<T>::m_Instance = nullptr;

class Manager: public Singleton <Manager>
{
public:
    void Print() const
    {
        cout << "Singleton Manager Successfully Printing!";
    }
};

int main(int argc, char* argv[])
{
    new Manager();
    Manager& manager{ Manager::GetSingleton() };
    manager.Print();
    delete Manager::GetSingletonPtr();

    return 0;
}

Result


Related Tutorials