1
我在我的entityManager中有以下一段代码。查询关于智能指针的正确使用
void GameObjectManager::updateAll(float frametime)
{
checkAlive();
// iterate through the entityManager, updating each entity and then adding it into the quadTree.
for (auto itr = _gameObjects.begin(); itr != _gameObjects.end(); itr++)
{
itr->second->Update(frametime);
tree->AddObject(itr->second);
}
// iterate once again through the entityManager but this time checking itself against the quadTree.
for (auto x = _gameObjects.begin(); x != _gameObjects.end(); x++)
{
std::vector<std::unique_ptr<Entity>> returnObjects = tree->GetObjectsAt(x->second->GetPosition().x, x->second->GetPosition().y);
for (int n = 0; n < (int)returnObjects.size(); n++)
{
std::unique_ptr<Entity>& collided = returnObjects[n];
if(object->getEntityName() != collided->getEntityName())
{
if(object->GetBoundingRect().intersects(collided->GetBoundingRect()))
{
object->Touch(collided);
}
}
}
}
tree->Clear();
}
这个例子中智能指针的正确使用是什么?当我将实体添加到四叉树时,我应该创建一个shared_ptr,将它作为参考传递或使用std :: move?我倾向于前两者之一,因为移动指针的所有权将它从std :: map移开,这是我不想做的事情。
传递信息时,我应该遵循哪些简单的规则?什么时候应该使用引用,什么时候应该使用shared_ptr?