小编Dog*_*nds的帖子

12
推荐指数
1
解决办法
8453
查看次数

使用唯一指针向树添加节点

我是智能指针的新手,我正在尝试创建一个双树,其中子节点通过唯一指针从父节点指向,并且子节点通过原始指针指向父节点.因此,当父节点被销毁时,整个子树将在该过程中被销毁.

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天前的智能指针(实际上有足够的时间学习一些东西).

c++ smart-pointers

2
推荐指数
1
解决办法
551
查看次数

标签 统计

c++ ×1

integer-division ×1

rust ×1

smart-pointers ×1