单例模式模板

#include<bits/stdc++.h>
using namespace std;
template <typename T>
class Singleton
{
public:
    static T * instance()
    {
        static T * instance =new T();
        return instance;
    }
protected:
    Singleton() {
    }
    Singleton(const Singleton<T> &);
    Singleton<T> & operator = (const Singleton<T> &);
    ~Singleton() {

    }
};
class myclass
{
    public:
        myclass(){
            std::cout << "constructor called!" << std::endl;
        };
        ~myclass(){
            std::cout << "destructor called!" << std::endl;
        };

};
int main()
{
    {
         myclass *a=Singleton<myclass>::instance();
        myclass *b=Singleton<myclass>::instance();
    }

    system("pause");
    return 0;
}

 

posted @ 2022-09-01 09:46  lhclqslove  阅读(19)  评论(0编辑  收藏  举报