我正在构建一棵树,其中 Node 类在左右子节点上具有 unique_ptr,并且 Node* 指针指向父节点。当我删除节点时,我获取节点并且我必须检查我要删除的节点是否在左子节点的右侧,然后重置父节点中的 unique_ptr。有没有办法获取指针并询问它周围是否有任何 unique_ptr 包装器并可能返回它?
最佳答案
Is there any way to take the pointer and ask if there is any unique_ptr wrapper around it and possibly return it?
没有找到 unique_ptr
的通用方法,但是您可以存储一个引用。
假设你的树是二叉树,你可以像这样在 parent 中找到 unique_ptr
:
(parent->left == this ? parent->left : parent->right).release();
如果树不是二叉树,您可以遍历所有子树。
关于c++ - 我可以获得指针所属的unique_ptr(如果有的话)吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47875138/