I'm confused as to why the object in my containers isn't reflecting the changes I make when I get the object by ref and call a function that changes that objects internal data member value. I know that I can store pointers in the container and return pointers from the Storage class but I was curious as to why returning references doesn't work the way I'm thinking it would.
class AI : public Component
{
private:
int testing;
public:
AI() { testing = 2;}
AI(int id) : Component(id) { testing = 5;}
virtual void Update()
{
}
void SetTesting(int v) { testing = v; }
int GetTesting() { return testing; }
};
//--------------------------------
template<class T>
class Storage
{
private:
map<int, T> data;
public:
T& Add(Object& obj)
{
int id = obj.GetID();
// make sure this object can only have 1 of this type
if(data.find(id) == data.end())
{
data[id] = T(obj.GetID());
}
return data[id];
}
T& Get(Object& obj)
{
return data[obj.GetID()];
}
void Update()
{
map<int, T>::iterator iter;
for(iter = data.begin(); iter != data.end(); ++iter)
(*iter).second.Update();
}
};
//--------------------------------
int main()
{
Storage<AI> ai;
Object o1;
AI a1 = ai.Add(o1); // get the reference of the AI object
a1.SetTesting(10); // change it's internal member variable to 10
// however at this point if I mouse over ai it's still showing the internal variable as 5. I would think that because .Add() returned a reference to the object in the container that any changes I make would be done to that object in the container
return 0;
}