C++11 take ownership of unique_ptr by the raw pointer or reference?

c++, c++11, pointers

Solution

To transfer ownership you need to have access to the `unique_ptr` as the raw pointer knows nothing of how it is being used.

Would it work to have the remove child method return the `unique_ptr` so that the object is kept alive during the transfer and the `Sprite` will be able to take the ownership? This will allow references to be used in other places as you have already.

Problem

Edit I suppose the code below would assume I have an overloaded version of addChild() that accepts a Sprite already wrapped in a unique_ptr, where taking ownership would be fine. Just thought I'd mention that before someone else did. :) . I made up all the code here after a very long day, so please take it as pseudo code quality meant only to demonstrate the issue at hand. Original Question I'm writing a framework where there is a display list, parents/children etc. I'm thinking that using `unique_ptr<Sprite>` for example is the way to go here, since when you add a child to a parent display object, it's only logical that the parent now becomes the sole owner of that child. However, there will be methods available such as `getChildAt(index)` and `getChildByName`, etc, which I believe should return a reference or pointer value, since these methods are simply meant to expose a child to operations, not transfer ownership. Finally, the issue and the reason for this question, is in the following situation. Lets assume we have two `Sprite` objects which are children of the root of display list, `Stage`. Let's say we have a third child Sprite. ``` Stage newStage; std::unique_ptr<Sprite> parentOne(new Sprite); std::unique_ptr<Sprite> parentTwo(new Sprite); newStage.addChild(parentOne); //Stage takes ownership of parentOne newStage.addChild(parentTwo); //Stage takes ownership of parentTwo std::unique_ptr<Sprite> someChild(new Sprite); parentOne->addChild(someChild) //parentOne takes ownership of someChild. ``` Now, somewhere else lets say in the code base of the game or whatever using this framework, `someChild` is accessed via `getChildAt(int index);`. ``` Sprite& child = parentOne->getChildAt(0); ``` It would be perfectly legal for the following to then happen. ``` parentTwo->addChild(child); ``` The `addChild` method handles removing the child from it's previous parent, if a parent exists, so that the new parent can now make this child part of its section of the display list. I'm returning the child(ren) from each sprite as a reference or pointer, because we don't want to hand off ownership (in methods such as `getChildAt()`), just provide access to the child. We don't want to hand it off as a `unique_ptr` and have it fall out of scope and die. However, as I say, it would be perfectly legal and normal for this child (now accessed by a reference or pointer) to be passed off to another container (lets say in a drag and drop operation, dragging an item from one list to another). The problem we have now is that the unique ownership needs to be transferred from one parent to another, but we only have a reference or raw pointer. I'm wondering what a sane solution to this problem would be. If I return a pointer, is it possible to transfer the ownership correctly at this stage? ``` void Sprite::addChild(Sprite* newChildToOwn) { /* by checking newChildToOwn->parent we can see that the child is already owned by someone else. We need to not only remove the child from that parents' part of the display list and add it here, but transfer exclusive object ownership of newChildToOwn to this->. */ } ```

Original source