G
Guest
Hi!
Please read the code snippet below. What it is supposed to produce a chain:
first_node(value = 0) --> next_node(value =5) --> next_node(value =10) -->
next_node(value = 15)
However, I fear that objects created in deep levels of recursions are
ultimately detroyed, although they are returned one level up. So, please help
me if you know how to rectify this code and achieve the desired result. I
need all the objects created during the recursions to be alive when I get
back to the top level.
Thanks in advance,
Ido
===========================================
class node
{
public:
node * next_node;
int value;
node() {
next_node = NULL;
value = 0;
};
};
node MyRecursive(node previous_node)
{
node current_node;
current_node.value = (previous_node.value + 5);
previous_node.next_node = &(current_node);
if (current_node.value < 20) MyRecursive(current_node);
else return previous_node;
}
int _tmain(int argc, _TCHAR* argv[])
{
node first_node;
first_node = MyRecursive(first_node);
return first_node.value;
}
Please read the code snippet below. What it is supposed to produce a chain:
first_node(value = 0) --> next_node(value =5) --> next_node(value =10) -->
next_node(value = 15)
However, I fear that objects created in deep levels of recursions are
ultimately detroyed, although they are returned one level up. So, please help
me if you know how to rectify this code and achieve the desired result. I
need all the objects created during the recursions to be alive when I get
back to the top level.
Thanks in advance,
Ido
===========================================
class node
{
public:
node * next_node;
int value;
node() {
next_node = NULL;
value = 0;
};
};
node MyRecursive(node previous_node)
{
node current_node;
current_node.value = (previous_node.value + 5);
previous_node.next_node = &(current_node);
if (current_node.value < 20) MyRecursive(current_node);
else return previous_node;
}
int _tmain(int argc, _TCHAR* argv[])
{
node first_node;
first_node = MyRecursive(first_node);
return first_node.value;
}