Ragazzi sono alle prese con questo problema e non riesco a capire perchè.
codice:
class Entity {
public:
template <class T> void addComponent(){
T *newComponent = new T();
this->mComponents[type_index(typeid(T))] = newComponent;
newComponent->entity = this;
newComponent->init();
}
template <class T> T* getComponent(){
if (this->mComponents.count(type_index(typeid(T))) > 0)
return static_cast<T*>(this->mComponents[type_index(typeid(T))]);
else
return NULL;
}
private:
map<type_index, IComponent*> mComponents;
}
int main() {
Entity *test = new Entity();
// qui non stampa "ok" poichè non trova alcun elemento con quel indice.
if (test->mComponents.count(type_index(typeid(AI_followTarget))) > 0)
cout << "ok";
// QUI DOVREBBE DARE ERRORE,
// test->getComponent<AI_followTarget>() dovrebbe restituire NULL dato che
// non l'ho inserito nella mappa
// invece mi stampa "ciao sei in test()"(metodo per testare)
test->getComponent<AI_followTarget>()->test();
system("pause");
return 0;
}
Dove ho commentato nel main , mi restituisce un elemento che io nella mappa non ho
inserito. Come mai lo trova comunque??
Grazie anticipatamente.