does anyone have parent_ptr?

Discussion in 'C++' started by Ron Natalie, Jul 3, 2003.

  1. Ron Natalie

    Ron Natalie Guest

    "Dhruv" <> wrote in message news:p...
    > I'd like to know if anyone had some sort of code like parent_ptr that I
    > could use. Let me just describe what I'm looking for. It's kinda a hybrid
    > between auto_ptr, and shared_ptr. You can specify who is the parent of the
    > object pointed to. Then, only the parent object can give someone the
    > parent-ship, and it itself relinquishes ownership, so only when the
    > parent_ptr which is a parent dies, will the memory be released.


    You've lost me...how does this object know when teh parent dies?
     
    Ron Natalie, Jul 3, 2003
    #1
    1. Advertising

  2. Ron Natalie

    Dhruv Guest

    I'd like to know if anyone had some sort of code like parent_ptr that I
    could use. Let me just describe what I'm looking for. It's kinda a hybrid
    between auto_ptr, and shared_ptr. You can specify who is the parent of the
    object pointed to. Then, only the parent object can give someone the
    parent-ship, and it itself relinquishes ownership, so only when the
    parent_ptr which is a parent dies, will the memory be released.

    -Dhruv.
     
    Dhruv, Jul 3, 2003
    #2
    1. Advertising

  3. Ron Natalie

    Yamin Guest

    "Dhruv" <> wrote in message
    news:p...
    > I'd like to know if anyone had some sort of code like parent_ptr that I
    > could use. Let me just describe what I'm looking for. It's kinda a hybrid
    > between auto_ptr, and shared_ptr. You can specify who is the parent of the
    > object pointed to. Then, only the parent object can give someone the
    > parent-ship, and it itself relinquishes ownership, so only when the
    > parent_ptr which is a parent dies, will the memory be released.
    >
    > -Dhruv.


    hey there,

    If the main requirement of the relationship is that the child dies after the
    parent does, then you'd probably be better off maintaining a child-list.
    Each parent would have a list of children. When it dies, it deletes all its
    children or sends a message to the child telling it to delete itself.

    Of course, in addition to this, each child could contain a pointer to its
    parent, but that would really only be used for ID purposes.

    Yamin
     
    Yamin, Jul 3, 2003
    #3
  4. Ron Natalie

    Howard Guest

    "Dhruv" <> wrote in message
    news:p...
    > I'd like to know if anyone had some sort of code like parent_ptr that I
    > could use. Let me just describe what I'm looking for. It's kinda a hybrid
    > between auto_ptr, and shared_ptr. You can specify who is the parent of the
    > object pointed to. Then, only the parent object can give someone the
    > parent-ship, and it itself relinquishes ownership, so only when the
    > parent_ptr which is a parent dies, will the memory be released.
    >
    > -Dhruv.


    That wouldn't work. The parent needs to delete the child (preferably in
    it's destructor).

    What would the child have as the pointer's type? And how would it use that
    pointer to know that the parent has died?

    If you want to "pass around" the child, you can simply tell parent#2 to
    point ot the child object pointed to by parent#1, then tell parent#1 to set
    its pointer to NULL. Then, when parent#2 gets destroyed, it will destroy
    the chil because it has a valid pointer to it (and parent#1 doesn't).

    Howard
     
    Howard, Jul 3, 2003
    #4
  5. Ron Natalie

    Dhruv Guest

    On Thu, 03 Jul 2003 23:14:50 +0500, Dhruv wrote:

    To all who have asked me about when the parent dies......

    Ok, it's something like this. If the parent dies, then ONLY will the
    memory be released. The parent need not ensure that it's children are
    dead/alive/etc............

    So, that means that if a chile and parent have the same pointer, then the
    parent dies. It means that the child will be left pointing to a deleted
    object. This sounds stupid, but that's what I require.


    -Dhruv.
     
    Dhruv, Jul 4, 2003
    #5
  6. Ron Natalie

    tom_usenet Guest

    On Thu, 03 Jul 2003 23:14:50 +0500, "Dhruv" <> wrote:

    >I'd like to know if anyone had some sort of code like parent_ptr that I
    >could use. Let me just describe what I'm looking for. It's kinda a hybrid
    >between auto_ptr, and shared_ptr. You can specify who is the parent of the
    >object pointed to. Then, only the parent object can give someone the
    >parent-ship, and it itself relinquishes ownership, so only when the
    >parent_ptr which is a parent dies, will the memory be released.


    I think you could do something like this with boost's weak_ptr. The
    real owners of an object have shared_ptrs to it, and things that don't
    want to extend the objects life hold weak_ptrs. However, you can't
    pass around ownership - shared_ptrs are owners and weak_ptrs aren't.

    Tom
     
    tom_usenet, Jul 4, 2003
    #6
    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. underground
    Replies:
    5
    Views:
    4,215
    underground
    Jul 22, 2004
  2. TaeHo Yoo
    Replies:
    2
    Views:
    441
    David
    Oct 24, 2003
  3. Leo Muller
    Replies:
    2
    Views:
    375
    Leo Muller
    Feb 12, 2004
  4. =?Utf-8?B?SmFja08=?=
    Replies:
    0
    Views:
    397
    =?Utf-8?B?SmFja08=?=
    Jun 30, 2005
  5. =?Utf-8?B?SmFja08=?=
    Replies:
    3
    Views:
    399
    Bruce Barker
    Oct 7, 2005
Loading...

Share This Page