#pragma once
template<typename T>
class Singleton
{
public:
static T* GetInstance()
{
if (!m_sInstance)
{
m_sInstance = new T();
}
return m_sInstance;
}
Singleton(T&&) = delete;
Singleton(const T&) = delete;
void operator= (const T&) = delete;
private:
class DestoryInstance
{
public:
~DestoryInstance()
{
if (m_sInstance)
{
delete m_sInstance;
m_sInstance = nullptr;
}
}
};
static DestoryInstance m_sDestory;
virtual void* GetDestoryInstance()
{
return &m_sDestory;
}
protected:
Singleton() = default;
virtual ~Singleton() = default;
private:
static T* m_sInstance;
};
template<class T>
T* Singleton<T>::m_sInstance = nullptr;
template<class T>
typename Singleton<T>::DestoryInstance Singleton<T>::m_sDestory;
#define DECL_SINGLETOPN(T) friend class Singleton<T>;
C++ 模板实现单例
最新推荐文章于 2024-08-22 00:23:00 发布