为什么左右节点地址相同?

测试数据 5,3,7
在输入3时,创建临时节点temp,值为3,将其插入左节点
输入7时,创建临时节点temp,值为7,但左节点的值会立刻变成7,但这不是两个函数中的两个不同变量吗?为什么地址会共用?
应该怎么修改呢?

# include<iostream>
# include<vector>

using namespace std;

class Node{
private:
    int m_value;
    Node *m_parent;
    Node *m_left=nullptr, *m_right=nullptr;
public:
    Node(int value){
        m_value = value;
    }
    Node(int value, Node* parent){
        m_value = value;
        m_parent = parent;
    }

    void setValue(int value){
        m_value = value;
    }
    void setParent(Node* parent){
        m_parent = parent;
    }
    void setLeftChild(Node* left){
        m_left = left;
    }
    void setRightChild(Node* right){
        m_right = right;
    }

    int getValue(){
        return m_value;
    }
    Node* getParent(){
        return m_parent;
    }
    Node* getLeftChild(){
        return m_left;
    }
    Node* getRightChild(){
        return m_right;
    }
};

class BinaryTree{
private:
    Node *root = nullptr;

public:
    void put(int value){
        if (root == nullptr){
            Node temp(value);  // value = 5
            root = &temp;
        }
        else{
            _put(value, root);
        }
    }

    void _put(int value, Node *current_node){
        // 递归
        if (value < current_node->getValue()){
            if (current_node->getLeftChild() == nullptr){
                Node temp(value, current_node);  // value = 3
                current_node->setLeftChild(&temp);
            }
            else{
                _put(value, current_node->getLeftChild());
            }
        }

        else if (value > root->getValue()){
            if (current_node->getRightChild() == nullptr){
                Node temp(value, current_node);  // value = 7
                // 但此时左子节点的地址与右子节点相同,左子节点value也变为7
                current_node->setRightChild(&temp);
            }
            else{
                _put(value, current_node->getRightChild());
            }
        }
    }
};

int main(){
    vector<int> nums = {5, 3, 7, 1, 4, 6, 8};
    BinaryTree tree;

    for(int i=0; i<nums.size(); i++){
        tree.put(nums[i]);
    }

    return 0;
}

我的天,我反正是没有这样用过,你知道对象有作用域之说,变量的有效作用域吧。
你如果定义一个局部对象,那么对象就只在这个代码块中生效。
那么你有没有想过,你用局部对象的地址,给一个类对象赋值,如果局部对象函数调用完毕,这个局部对象空间还在吗? 计算机会不会覆盖使用这个空间?

img


你为什么觉得这样可以呢? 人家可以,是因为人家内部实际上是new了一个对象,只是把你传参的对象的值拷贝进去。
你这样写,出了这个函数作用域,这个局部对象还怎么生效?