关于函数内部要改变指针的指向问题

#include<iostream>
#include <ranges>
#include <type_traits>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>


using namespace std;

class TreeNode {
public:
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int val) : val(val) {}
};
####################################################################################
void printTree(TreeNode*& cur/****************需要用引用才能改变指针指向*********************/) {
    cout << "传进函数的地址cur: " << cur << endl;
    cur = new TreeNode(12);
    cout << "函数内部的值: " << cur->val << endl;
}


int main() {
    TreeNode* root = new TreeNode(1);
    cout << "root的地址: " << root << endl;
    printTree(root);
    cout << "函数外部的值: " << root->val << endl;
    



    return 0;
}

posted @ 2024-04-14 01:53  铜锣湾陈昊男  阅读(9)  评论(0)    收藏  举报