does anyone have parent_ptr?

R

Ron Natalie

Dhruv said:
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?
 
D

Dhruv

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

Yamin

Dhruv said:
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
 
H

Howard

Dhruv said:
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
 
D

Dhruv

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

tom_usenet

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top