我是智能指针的新手,我正在尝试创建一个双树,其中子节点通过唯一指针从父节点指向,并且子节点通过原始指针指向父节点.因此,当父节点被销毁时,整个子树将在该过程中被销毁.
class Node {
private:
Node *parent;
std::unique_ptr<Node> left;
std::unique_ptr<Node> right;
public:
Node(Node* _left, Node* _right, Node* _parent);
};
Node::Node(Node* _left, Node* _right, Node* _parent) {
parent = &_parent;
//this is where the problem starts
}
Run Code Online (Sandbox Code Playgroud)
我不明白如何指向一个可能有我要连接的树的新节点.如果我使用make_unique,我相信会创建一个新节点而不是保留树.
我可能完全错了,因为我刚刚学习了4天前的智能指针(实际上有足够的时间学习一些东西).