*& dereferencing a pointer to a class

P

Peter L.

Hi!

I would like to know if it's allowed to dereference a pointer
to a class like im doing it. I'm building a tree with nodes.

class node
{
private:
int num;

public:
vector<string> values;
node *left;
node *right;
node *father;

node();
};

node:: node()
{
num = 0;
}

int add(node *& father, node *& root) // here is it!
{
// here is some other code

if(root==NULL)
{
root = new node;

// here I do some operations with the values

root->left = NULL;
root->right = NULL;
root->father = father; // set a pointer to his father

}
else
{
bool valuesequal = false;

// here I have some code to check if I have the same values
// in the node
// and in a variable that I get from elsewhere

if(valuesequal)
{
add(root, root->left);
return 0;
}
else
{
add(root, root->right);
return 0;
}
}
return 0;
}

I've seen that this operation with *& has some quite bizarre effects in
the memory when I changed two nodes in another function.
Is there a better way to do it?

I would appreciate any comment.

Thank you.

Peter
 
J

Jakob Bieling

I would like to know if it's allowed to dereference a pointer
to a class like im doing it. I'm building a tree with nodes.
int add(node *& father, node *& root) // here is it!

You are *not* dereferincing a pointer here. You just said 'father' and
'root' are references to pointers to a 'node'. That means your function
'add' can change the pointer values (like you did) and the objects pointed
to as well.
I've seen that this operation with *& has some quite bizarre effects in
the memory when I changed two nodes in another function.
Is there a better way to do it?


Can you describe what kind of 'bizarre' effects you mean? If you are
talking about the function's ability to change the pointer, then don't pass
a reference to a pointer, but a pointer instead.

hth
 
P

Peter L.

Thank you for your comment.
You're right, I'm just using a reference to the pointer.

The strange effect was in this function that
swaps to nodes.

int movenode(node * root)
{

if (root->right != NULL)
{
root->right->father = root->father;
}
root->father->right = root->right;

if (root->father->father != NULL)
{
root->father->father->left = root;
}
root->right = root->father;
root->father = root->father->father;

if (root->right != NULL)
{
root->right->father = root;
}

return 0;
}

When I used a reference to a pointer on the top (movenode(node *& root)),
the function couldn't swap correctly the two nodes.
In the middle of the function the pointer to root jumped to another node,
although I didn't change it.
 
J

Jakob Bieling

Peter L. said:
Thank you for your comment.
You're right, I'm just using a reference to the pointer.

The strange effect was in this function that
swaps to nodes.

int movenode(node * root)
{

if (root->right != NULL)
{
root->right->father = root->father;
}
root->father->right = root->right;

if (root->father->father != NULL)
{
root->father->father->left = root;
}
root->right = root->father;
root->father = root->father->father;

if (root->right != NULL)
{
root->right->father = root;
}

return 0;
}

When I used a reference to a pointer on the top (movenode(node *& root)),
the function couldn't swap correctly the two nodes.
In the middle of the function the pointer to root jumped to another node,
although I didn't change it.

As you said, you didn't change it. So the problem probably lies
elsewhere in your code. In the above function, it makes no difference if the
argument is a pointer or a reference to a pointer.

hth
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top