我正在尝试实现 B+ 树。这是实际代码的一小部分。将对象指针传递给函数时遇到了一些问题。据我所知,在函数内部创建的那些对象随后会被销毁。那么,在不改变语义并保持函数递归的情况下改进这一点的好方法是什么。对于任何反馈,我们都表示感谢。
void percolate_up(IndexNode* Current, int btree_order, IndexNode* Right, IndexNode* Root)
{
if(Current->Parent == NULL)
{
IndexNode* UpperNode = new IndexNode;
UpperNode->AddChild(Current);
UpperNode->AddChild(Right);
Current->Parent = UpperNode;
Right->Parent = UpperNode; //This is defined inside an if statement
// in main yet this statement doesn't affect it
UpperNode->AddKey(Current->Keys[btree_order]);
Root = UpperNode;
}
else if(.......){
...
...
percolate_up(....);
}
}
int main(){
...
if(...){
IndexNode* RightNode = new IndexNode;
percolate_up(Current, btree_order, RightNode, Root);
//RightNode->Parent is still NULL here but Current->Parent is changed
//Also Root is unchanged, Why does this happen?
}