*& dereferencing a pointer to a class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter L.

    *& dereferencing a pointer to a class

    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
  • Jakob Bieling

    #2
    Re: *&amp; dereferencing a pointer to a class

    "Peter L." <plienhard@blue win.ch> wrote in message
    news:3c1384f0.0 402161258.70562 7ce@posting.goo gle.com...
    [color=blue]
    > 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.[/color]
    [color=blue]
    > int add(node *& father, node *& root) // here is it![/color]

    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.
    [color=blue]
    > 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?[/color]


    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)


    Comment

    • Peter L.

      #3
      Re: *&amp; dereferencing a pointer to a class

      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.

      Comment

      • Jakob Bieling

        #4
        Re: *&amp; dereferencing a pointer to a class

        "Peter L." <plienhard@blue win.ch> wrote in message
        news:3c1384f0.0 402170233.64bc4 8cf@posting.goo gle.com...[color=blue]
        > 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.[/color]

        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)


        Comment

        Working...