40
testHasttable/singletonbase.h
Normal file
40
testHasttable/singletonbase.h
Normal file
@@ -0,0 +1,40 @@
|
||||
#ifndef SINGLETONBASE_H
|
||||
#define SINGLETONBASE_H
|
||||
|
||||
//做一个单例
|
||||
#include <QHash>
|
||||
template<typename KeyType,typename ValueType>
|
||||
class SingletonBase
|
||||
{
|
||||
public:
|
||||
SingletonBase();
|
||||
~SingletonBase();
|
||||
void add(const KeyType& key,const ValueType& val)
|
||||
{
|
||||
this->ht[key]=val;
|
||||
}
|
||||
bool contains(const KeyType& key)
|
||||
{
|
||||
return this->ht->contains(key);
|
||||
}
|
||||
ValueType get(const KeyType& key)
|
||||
{
|
||||
return this->ht[key];
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
class CG // 它的唯一工作就是在析构函数中删除CSingleton的实例
|
||||
{
|
||||
public:
|
||||
~CG()
|
||||
{
|
||||
if (SingletonBase::ht)
|
||||
delete SingletonBase::ht;
|
||||
}
|
||||
};
|
||||
static CG Garbo; // 定义一个静态成员,在程序结束时,系统会调用它的析构函数
|
||||
static QHash<KeyType,ValueType> *ht; *ht;
|
||||
};
|
||||
|
||||
#endif // SINGLETONBASE_H
|
||||
Reference in New Issue
Block a user