#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; }
标签:Singleton,const,模式,instance,static,单例,myclass,public,模板 From: https://www.cnblogs.com/lhclqslove/p/16645440.html