Ciao a tutti,
Ho questa situazione:
Io mi aspettavo che venisse stampato a video 3.0 ...invece stampa 0. Mi viene il dubbio che p è una copia invece dell'elemento dentro il vettore. Quancuno mi dice dove sbaglio ?codice://components.h typedef struct structPositionComponent{ float x; float y; float z; structPositionComponent(){ x = y = z = 0; } }PositionComponent; //componentmanager.h template <typename T> class ComponentManager { public: ComponentManager(){}; virtual ~ComponentManager(){}; T& getComponent(Entity* e){ if(this->_map.find(e) == this->_map.end()){ return create(e); } return this->components[this->_map[e]]; }; T& create(Entity* e){ if(this->_map.find(e) == this->_map.end()){ T data; this->components.push_back(data); this->_map[e] = this->components.size() - 1; return this->components[this->components.size() - 1]; }else{ return this->components[this->_map[e]]; } } protected: private: std::vector<T> components; std::map<Entity*, unsigned int> _map; }; //main.cpp int main(){ ComponentManager<PositionComponent> posManager; Entity the_entity; PositionComponent p = posManager.create(&the_entity); p.x = 3.0f; p.y = 3.0f; p.z = 2.1f; PositionComponent ptest = posManager.getComponent(&the_entity); std::cout << "->" << (float)ptest.x << std::endl; }Devo per forza usare un puntatore?
PS:
Se invece di fare
faccocodice:T data; this->components.push_back(data);
E' la stessa cosa a livello di memoria?codice:this->components.push_back(new T);
Grazie

Devo per forza usare un puntatore?
Rispondi quotando

