Apr 23, 2015 at 1:35pm UTC
I have the code below with explanation below.
All objects array:
Object* m_objects[cMaximumNumberOfObjects];
CombatObject:
class CombatObject : public Object
{
public:
CombatObject()
{
m_position = new float[3];
}
~CombatObject()
{
delete[] m_position;
}
float* GetPosition() { return m_position; }
void SetPosition(float* aPosition);
float* m_position;
};
Function in question:
bool ObjectManager::SetCombatObjectInactive(unsigned int aObjectId)
{
MutexLock lock(m_lock);
for(unsigned int i = 0; i < m_numberOfObjects; i++)
{
if(m_objects[i]->m_objectId == aObjectId)
{
CombatObject* pCombatObject = new CombatObject();
pCombatObject->m_objectId = m_objects[i]->m_objectId;
pCombatObject->m_bitFlags = m_objects[i]->m_bitFlags;
pCombatObject->SetActive(false);
pCombatObject->SetPosition(m_objects[i]->GetPosition());
delete m_objects[i];
m_objects[i] = pCombatObject;
return true;
}
}
return false;
}
I'm trying to figure out why these operations are setup like this. I would think it would be more efficent to grab a tempReference to the object at 'i', and modify the tempRef rather then allocating a new object, copy over all properties, then reassigning the object to 'i'.
Something like:
if(m_objects[i]->m_objectId == aObjectId)
{
CombatObject* pCombatObject = (CombatObject)m_objects[i];
pCombatObject->SetActive(false);
return true;
}
Anyone mind schooling me?