#include <iostream>
#include <cstring>
using namespace std;
template<class T>
class Sing
{
public:
Sing();
virtual ~Sing();
static T* getInstance();
static void destory();
private:
static T* instance;
};
template<class T>
T* Sing<T> :: instance = NULL;
template<class T>
T* Sing<T> :: getInstance()
{
if(NULL == instance)
{
instance = new T();
}
return instance;
}
template<class T>
void Sing<T>::destory()
{
if(NULL != instance)
{
delete instance;
instance = NULL;
}
};
class TestSing
{
public:
TestSing()
{
str = Sing<char>::getInstance();
if(str != NULL)
{
cout << "single test is ok"<<endl;
}
}
virtual ~TestSing()
{
Sing<char>::destory();
if(str == NULL)
{
cout << "single destory is ok"<< endl;
}
}
private:
char* str;
};
int main(int argc, char* argv[])
{
TestSing t;
return 0;
}
template + single instance
最新推荐文章于 2022-02-16 14:46:23 发布