safe deletion of pointer, trees

Discussion in 'C++' started by Christopher Pisz, Sep 3, 2004.

  1. currently my node is like so with everything public:

    class Quadtree_Node
    {
    public:
    Quadtree_Node(Quadtree_Node * parent, Bounding_Box & bounds);
    ~Quadtree_Node();
    void Subdivide(float smallest_node);
    const Quadtree_Node *& GetTopLeft();
    Bounding_Box m_bounds; // Bounding Coordinates of this node
    Quadtree_Node * m_parent; // Parent of this node, NULL if root
    Quadtree_Node * m_tlchild; // Top Left child node
    Quadtree_Node * m_trchild; // Top Right child node
    Quadtree_Node * m_blchild; // Bottom Left child node
    Quadtree_Node * m_brchild; // Bottom Right child node
    std::list<Data> m_data; // Linked List of Data
    };

    And my deletion method looks like:

    void Quadtree::Delete(Quadtree_Node * node)
    {
    if(node)
    {
    Delete(node->m_tlchild);
    Delete(node->m_trchild);
    Delete(node->m_blchild);
    Delete(node->m_brchild);
    }
    delete node;
    }

    How can I change this so that I can make my data members of class
    Quadtree_Node private? I was thinking of making a GetFunction to get the
    pointers to the children of the node, but then I am not sure if I would be
    returning a copy of the pointer, a referance of the pointer or the pointer
    itself for use in the deletion.

    A try:

    const Quadtree_Node *& GetTopLeft() {return m_tlchild;}
    delete ->node->GetTopLeft();

    Would that be safe?

    Thanks,
    Christopher
     
    Christopher Pisz, Sep 3, 2004
    #1
    1. Advertising

  2. Christopher Pisz

    Howard Guest

    "Christopher Pisz" <> wrote in message
    news:chai76$fe7$...
    > currently my node is like so with everything public:
    >
    > class Quadtree_Node
    > {
    > public:
    > Quadtree_Node(Quadtree_Node * parent, Bounding_Box & bounds);
    > ~Quadtree_Node();
    > void Subdivide(float smallest_node);
    > const Quadtree_Node *& GetTopLeft();
    > Bounding_Box m_bounds; // Bounding Coordinates of this node
    > Quadtree_Node * m_parent; // Parent of this node, NULL if root
    > Quadtree_Node * m_tlchild; // Top Left child node
    > Quadtree_Node * m_trchild; // Top Right child node
    > Quadtree_Node * m_blchild; // Bottom Left child node
    > Quadtree_Node * m_brchild; // Bottom Right child node
    > std::list<Data> m_data; // Linked List of Data
    > };
    >
    > And my deletion method looks like:
    >
    > void Quadtree::Delete(Quadtree_Node * node)
    > {
    > if(node)
    > {
    > Delete(node->m_tlchild);
    > Delete(node->m_trchild);
    > Delete(node->m_blchild);
    > Delete(node->m_brchild);
    > }
    > delete node;
    > }


    You should, in general, allow the object being deleted to handle deleting
    its own members. So, you should simply call delete on node itself, and
    allow the destructor for the Quadtree_Node to delete all its members. Then,
    the members can be public, protected, or private, as you desire.

    Unless there is more code you haven't shown in the Delete function, you can
    simply remove that, and call delete directly on the node you were previously
    passing to Quadtree::Delete. The Quadtree::Delete function, as it is shown
    above, is simply not needed.

    You'll also probably want an assignment operator for your Quadtree_Node
    class. (Google for the "rule of three" to see why.)

    -Howard
     
    Howard, Sep 3, 2004
    #2
    1. Advertising

  3. "Christopher Pisz" <> wrote in message
    news:chai76$fe7$...
    > currently my node is like so with everything public:
    >
    > class Quadtree_Node
    > {
    > public:
    > Quadtree_Node(Quadtree_Node * parent, Bounding_Box & bounds);
    > ~Quadtree_Node();
    > void Subdivide(float smallest_node);
    > const Quadtree_Node *& GetTopLeft();
    > Bounding_Box m_bounds; // Bounding Coordinates of this node
    > Quadtree_Node * m_parent; // Parent of this node, NULL if root
    > Quadtree_Node * m_tlchild; // Top Left child node
    > Quadtree_Node * m_trchild; // Top Right child node
    > Quadtree_Node * m_blchild; // Bottom Left child node
    > Quadtree_Node * m_brchild; // Bottom Right child node
    > std::list<Data> m_data; // Linked List of Data
    > };
    >
    > And my deletion method looks like:
    >
    > void Quadtree::Delete(Quadtree_Node * node)
    > {
    > if(node)
    > {
    > Delete(node->m_tlchild);
    > Delete(node->m_trchild);
    > Delete(node->m_blchild);
    > Delete(node->m_brchild);
    > }
    > delete node;
    > }
    >
    > How can I change this so that I can make my data members of class
    > Quadtree_Node private? I was thinking of making a GetFunction to get the
    > pointers to the children of the node, but then I am not sure if I would be
    > returning a copy of the pointer, a referance of the pointer or the pointer
    > itself for use in the deletion.
    >


    There is no reason in the above code that you can't just make them private
    as it is. I mean what's the problem, Delete is a member of Quadtree so it is
    allowed to access private members of Quadtree, even if those are members of
    a different node. Try it and see.

    john
     
    John Harrison, Sep 4, 2004
    #3
    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. Aff@n
    Replies:
    1
    Views:
    366
    Ian Collins
    Oct 16, 2006
  2. Marcus

    Pointer deletion question

    Marcus, Feb 16, 2007, in forum: C++
    Replies:
    4
    Views:
    310
    Marcus
    Feb 16, 2007
  3. Replies:
    5
    Views:
    4,939
    Stuart Redmann
    Aug 9, 2007
  4. Edsoncv

    JNI and pointer deletion

    Edsoncv, Apr 23, 2008, in forum: Java
    Replies:
    2
    Views:
    551
    Gordon Beaton
    Apr 24, 2008
  5. jacob navia

    Binary search trees (AVL trees)

    jacob navia, Jan 3, 2010, in forum: C Programming
    Replies:
    34
    Views:
    1,433
    Dann Corbit
    Jan 8, 2010
Loading...

Share This Page