#include <iostream>
using namespace std;
template <typename T>
class Singleton
{
public:
int get_data();
static Singleton* &get_ptr();
public:
int m_data;
static Singleton *m_ptr;
private:
Singleton(){ m_data = 10; }
Singleton(const Singleton &);
Singleton &operator=(const Singleton &);
};
template <typename T>
Singleton<T>*Singleton<T>::m_ptr = nullptr;
template <typename T>
Singleton<T>* &Singleton<T>::get_ptr()
{
m_ptr = new Singleton<T>();
return m_ptr;
}
template <typename T>
inline int Singleton<T>::get_data()
{
return m_data;
}
int main()
{
Singleton<int> *ptr = Singleton<int>::get_ptr();
cout << ptr->get_data() << endl;
return 0;
}
单例
最新推荐文章于 2024-01-05 10:46:13 发布