*& dereferencing a pointer to a class

Discussion in 'C++' started by Peter L., Feb 16, 2004.

  1. Peter L.

    Peter L. Guest

    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
     
    Peter L., Feb 16, 2004
    #1
    1. Advertising

  2. "Peter L." <> wrote in message
    news:...

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

    (replace y with x if you want to reply by e-mail)
     
    Jakob Bieling, Feb 16, 2004
    #2
    1. Advertising

  3. Peter L.

    Peter L. Guest

    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.
     
    Peter L., Feb 17, 2004
    #3
  4. "Peter L." <> wrote in message
    news:...
    > 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
    --
    jb

    (replace y with x if you want to reply by e-mail)
     
    Jakob Bieling, Feb 17, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Larry Lindsey
    Replies:
    1
    Views:
    432
    Larry Lindsey
    Sep 24, 2003
  2. BigMan
    Replies:
    51
    Views:
    7,262
    Rolf Magnus
    Feb 17, 2005
  3. Richard Cavell
    Replies:
    4
    Views:
    7,033
    Karl Heinz Buchegger
    Feb 16, 2005
  4. Vam
    Replies:
    1
    Views:
    384
    Victor Bazarov
    Feb 18, 2005
  5. Pushkar Pradhan

    warning: dereferencing `void *' pointer

    Pushkar Pradhan, Dec 14, 2003, in forum: C Programming
    Replies:
    4
    Views:
    2,216
Loading...

Share This Page