私の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 から移動するため、最初の2つのうちの1つに傾いています。これは私がやりたくないことです。
情報を渡す際に従うべき簡単なルールはありますか? いつ参照を使用し、いつ shared_ptr を使用する必要がありますか?