I am having difficulties understanding the ownership of unique pointers. Can anyone explain how it is possible to store a pointer to a unique pointer in a set?
Example from SFML game dev book which illustrates the problem:
typedef std::pair<SceneNode*, SceneNode*> Pair;
typedef std::unique_ptr<SceneNode> Ptr;
std::vector<Ptr> mChildren;
void SceneNode::checkSceneCollision(SceneNode& sceneGraph, std::set<Pair>& collisionPairs)
{
checkNodeCollision(sceneGraph, collisionPairs);
for(Ptr& child : sceneGraph.mChildren)
checkSceneCollision(*child, collisionPairs);
}
void SceneNode::checkNodeCollision(SceneNode& node, std::set<Pair>& collisionPairs)
{
if (this != &node && collision(*this, node))
collisionPairs.insert(std::minmax(this, &node));
for(Ptr& child : mChildren)
child->checkNodeCollision(node, collisionPairs);
}
Does this not defy the meaning of a unique_ptr? Since it is possible to access the actual object from the set and modify it.