Reputation: 35
I am learning C++
I have a function:
void remove_node(node *&rem);
As I understood, rem
is a node*
, which is passed by reference. I do need it to be passed by reference, because I change the pointer.
But sometimes I need to pass it by value (recursive call). I did it like this:
node *noref = rem->left;
remove_node(noref);
instead of
remove_node(rem->left);
Is there better way to do it?
Thanks.
Upvotes: 1
Views: 47
Reputation: 26
What do you wish to do with your nodes ? What are the attributs & methods of the class ?
Usually when using nodes, you just change the links between the nodes, and not the nodes themselves (ex : double-linked list).
struct node {
node* right;
node* left;
};
typedef struct node node;
void remove_node(node* n) {
l_node = n->left;
r_node = n->right;
l_node->right = r_node;
r_node->left = l_node;
free(n);
}
that would be the C code, to pass it to C++ just use the accessors and the destructor :)
Upvotes: 1