Попытки создать объекты кэш различных БД экземпляров, так что только один экземпляр БДА хранитсяшаблона Функции Инстанцирование Проблемы с пользовательским типом данных
class objectCache
{
public:
static inline objectCache& getInstance()
{
static objectCache instance;
return instance;
}
template<typename K, typename V>
Db <K,V> * getDbInstance(string &obj_name);
private:
objectCache(){};
objectCache(const objectCache& copy);
objectCache& operator=(const objectCache& copy);
map <string,void *> mapObjCache;
};
template<typename K, typename V>
Db <K,V> *
objectCache::getDbInstance(string &Db_name)
{
Db<K,V> * lookup_db_ptr = NULL;
string Db_key = "Db_";
Db_key += Db_name;
map <string,void *>::iterator it;
it = mapObjCache.find(Db_key);
if ( mapObjCache.end() != it && NULL != mapObjCache[Db_key])
{
lookup_db_ptr = (Db<string,string> *)mapObjCache[Db_key];
return lookup_db_ptr;
}
else
{
try{
lookup_db_ptr = new Db<K,V>(Db_name,O_RDONLY);
}
catch(...){lookup_db_ptr = NULL;}
}
if (lookup_db_ptr)
{
try{
mapObjCache[Db_key] = (void *)lookup_db_ptr;
}
catch(...)
{
delete lookup_db_ptr;
lookup_db_ptr = NULL;
}
}
return lookup_db_ptr;
}
Некоторые, как определение терпит неудачу, когда я хотел бы создать объекты пользовательских определенных структур.
Ниже один работает
db_ptr = (Db<string,string> *) objectCache::getInstance().getDbInstance <string,string> (dbname);
в то время как с индивидуальным типом структуры, следующее определение не удается
typedef struct
{
float val1;
float val2;
short int test1;
short int test2;
}myData_t;
myData_t myData;
Db<string,myData_t> *db_ptr;
db_ptr = (Db<string,myData_t> *)
objectCache::getInstance().getDbInstance <string,myData_t> (dbname);
с ошибками
error: cannot convert `Db<std::string, std::string>*' to
`Db<std::string, myData_t>*' in assignment.
Что вы не понимаете? Сообщение об ошибке довольно ясно. Также вы действительно не хотите использовать эти указатели. Также используйте приведения стиля C++. Также почему вы используете синглтон? – Shoe