单例模式模板
#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; }