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
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