template用法(c++)
#include <stddef.h>
template<typename T>
class Singleton
{
public:
static T &getInstance ()
{
if(NULL == instance)
{
instance = new T();
}
return *instance;
}
static void delInstance ()
{
if(NULL != instance)
{
delete instance;
instance = NULL;
}
}
protected:
Singleton ()
{
}
~Singleton ()
{
}
private:
Singleton (const Singleton &)
{
}
Singleton &operator = (const Singleton &)
{
}
static T *instance;
};
template<typename T>
T *Singleton<T>::instance = NULL;
/* init*/
class BaseClient{};
class TestClient :public BaseClient, Singleton<MyClass>{};
BaseClient *pClient = NULL;
switch (id)
{
case INDEX1:
pClient = &TestClient::getInstance();
break;
}