Tirando un pò le somme.
Partiamo da qui:
codice:
map<string, CBaseEntityIfc>* pMap;
string m_strRefOwnerHistory;
CIfcOwnerHistory* getRefOwnerHistory()
{
(*pMap)[this->gettype()][m_strRefOwnerHistory];
}
La map è sbagliata. Infatti presuppone un oggetto fatto e finito di tipo CBaseEntityIfc quando invece dev'essere CBaseEntityIfc*,
visto che nella funzione getRefOwnerHistory() restituisci un puntatore.
Tra l'altro quella funzione non può compilare perché:
1) CIfcOwnerHistory non deriva da CBaseEntityIfc;
2) CIfcOwnerHistory deriva da CBaseEntityIfc, però il compilatore dice che non può effettuare il downcasting.
Se è questo il caso hai due possibilita:
1) Restituisci un puntatore a CBaseEntityIfc;
codice:
map<string, CBaseEntityIfc*>* pMap;
string m_strRefOwnerHistory;
CBaseEntityIfc* getRefOwnerHistory()
{
(*pMap)[this->gettype()][m_strRefOwnerHistory];
}
2) Effettui un dynamic_cast.
codice:
map<string, CBaseEntityIfc*>* pMap;
string m_strRefOwnerHistory;
CIfcOwnerHistory* getRefOwnerHistory()
{
CBaseEntityIfc* ptr = (*pMap)[this->gettype()][m_strRefOwnerHistory];
return dynamic_cast<CIfcOwnerHistory*>(ptr);
}
=================================================
La map.
Da come hai scritto, sembra più una multimap, però non cambia per il discorso generale.
Portarsi dietro un riferimento in quel modo mi sembra molto scomodo. (Chi è il reale possessore della mappa, incaricato della distruzione?)
Io opterei per rendere la map statica dentro la classe base. In questo modo è visibile a tutte le classi della gerarchia e puoi dimenticare
il puntatore.
codice:
class CBaseEntityIfc
{
public:
// typedef di comodo.
typedef std::map<std::string, CBaseEntityIfc*> RefOwnerMap;
CBaseEntityIfc(void);
virtual ~CBaseEntityIfc(void);
virtual string CBaseEntityIfc::getID();
virtual string CBaseEntityIfc::getType(){return "IfcBase";};
virtual string CBaseEntityIfc::setID(string strId){m_strId = strId;};
virtual string CBaseEntityIfc::setType(string strType){m_strType = strType;};
protected:
//l'id e il tipo sono comuni a tutte le classi IFC per questo le
//implemento nella classe base
string m_strId;
string m_strType;
// mappa statica
static RefOwnerMap refMap;
};
CBaseEntityIfc::RefOwnerMap CBaseEntityIfc::refMap;
e le classi derivate:
class CIfcProject :
public CBaseEntityIfc
{
public:
CIfcProject(void);
virtual ~CIfcProject(void);
CIfcOwnerHistory* getRefOwnerHistory()
{
return dynamic_cast<CIfcOwnerHistory*>(CBaseEntityIfc::refMap[this->gettype()][m_strREF]);
}
};