指向节点的指针的子节点总是设置为 NULL
childrens of a pointer pointing to a Node are always set to NULL
我正在使用递归调用创建一个 AI 游戏,我有一个名为 "Node" 的结构,当用新的 int[5][5] 填充它们时它们被填充但是当我 return 并调用其他函数,例如
当我调用一个名为 "bestMove" 的函数在 childrens 中循环时,它们都指向 NULL,例如:
struct Node
{
int state[5][5];
Node* parent;
Node* child[20];
Node()
{
parent = NULL;
for(int i=0;i<=19;i++)
child[i] = NULL;
}
Node(Node* father)
{
parent = father;
for(int i=0;i<=19;i++)
child[i] = NULL;
}
}
void fill_new_state(Node* childPointer)
{
if(everything is Ok) //and its Okay
childPointer = new Node();//with linking to parent and all...
childPointer->state[row][col] = bla bla bla//fill
return;
}
void expandChildern(Node* root)
{
fill_new_state(root->child[i])//looping
}
void bestMove(Node* root)
{
Node* best = root->child[0] //if not null and it is not null
//loop and chose the best
root = best
}
void main()
{
Node* root = new Node();
expandChildren(root);
bestMove(root);
}
在函数 fill_new_state(Node* childPointer)
中 child 被填充并且它不是 NULL
但是在 return 从 fill_new_state(Node* childPointer)
完全在函数 expandChildren(Node* root)
中 child 之后 child 是 NULL。
in the function fill_new_state(Node* childPointer) the child is filled
and it is not NULL but after returning from fill_new_state(Node*
childPointer) exactly in the function expandChildren(Node* root) the
child is NULL.
那是因为你按值传递了指针,它创建了指针的副本。然后在 fill_new_state 中将副本设置为指向新的 Node 对象,但这对函数外的任何其他指针都没有影响。
相当于这样做:
void foo(int val)
{
val = val + 1;
}
void bar()
{
int x = 5;
foo(x);
// You can't expect x to be 6 here, because foo() incremented a
// copy of x, not the original x.
}
获得所需行为的方法是通过引用传递指针参数:
void fill_new_state(Node* & childPointer)
... 这样,对 fill_new_state() 中的指针所做的任何更改也将在调用上下文中可见——即 root->child[i] 将在以下情况下设置为非 NULL fill_new_state returns.
我正在使用递归调用创建一个 AI 游戏,我有一个名为 "Node" 的结构,当用新的 int[5][5] 填充它们时它们被填充但是当我 return 并调用其他函数,例如 当我调用一个名为 "bestMove" 的函数在 childrens 中循环时,它们都指向 NULL,例如:
struct Node
{
int state[5][5];
Node* parent;
Node* child[20];
Node()
{
parent = NULL;
for(int i=0;i<=19;i++)
child[i] = NULL;
}
Node(Node* father)
{
parent = father;
for(int i=0;i<=19;i++)
child[i] = NULL;
}
}
void fill_new_state(Node* childPointer)
{
if(everything is Ok) //and its Okay
childPointer = new Node();//with linking to parent and all...
childPointer->state[row][col] = bla bla bla//fill
return;
}
void expandChildern(Node* root)
{
fill_new_state(root->child[i])//looping
}
void bestMove(Node* root)
{
Node* best = root->child[0] //if not null and it is not null
//loop and chose the best
root = best
}
void main()
{
Node* root = new Node();
expandChildren(root);
bestMove(root);
}
在函数 fill_new_state(Node* childPointer)
中 child 被填充并且它不是 NULL
但是在 return 从 fill_new_state(Node* childPointer)
完全在函数 expandChildren(Node* root)
中 child 之后 child 是 NULL。
in the function fill_new_state(Node* childPointer) the child is filled and it is not NULL but after returning from fill_new_state(Node* childPointer) exactly in the function expandChildren(Node* root) the child is NULL.
那是因为你按值传递了指针,它创建了指针的副本。然后在 fill_new_state 中将副本设置为指向新的 Node 对象,但这对函数外的任何其他指针都没有影响。
相当于这样做:
void foo(int val)
{
val = val + 1;
}
void bar()
{
int x = 5;
foo(x);
// You can't expect x to be 6 here, because foo() incremented a
// copy of x, not the original x.
}
获得所需行为的方法是通过引用传递指针参数:
void fill_new_state(Node* & childPointer)
... 这样,对 fill_new_state() 中的指针所做的任何更改也将在调用上下文中可见——即 root->child[i] 将在以下情况下设置为非 NULL fill_new_state returns.