gpt4 book ai didi

c++ - 函数参数 - 指针或对指针的引用?

转载 作者:太空狗 更新时间:2023-10-29 20:47:40 29 4
gpt4 key购买 nike

void DeleteChildren(BSTNode *node)
{
// Recurse left down the tree...
if(node->HasLeftChild()) DeleteChildren(node->GetLeftChild());
// Recurse right down the tree...
if(node->HasRightChild()) DeleteChildren(node->GetRightChild());

// Clean up the data at this node.
node->ClearData(); // assume deletes internal data

// Free memory used by the node itself.
delete node;
}

// Call this from external code.
DeleteChildren(rootNode);

这个函数递归地删除BST。

我对第一行 BSTNode *node 有疑问,我应该将它修改为 BSTNode *& node 吗?

最佳答案

您唯一希望通过引用传递指针的情况是您想要更改指针指向的内容。例如,如果您想在删除节点后将节点设置为 NULL,那么您需要作为 BSTNode*& 传递。

关于c++ - 函数参数 - 指针或对指针的引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5043718/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com