Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

weak_ptr to break cyclic reference

1 view
Skip to first unread message

Pallav singh

unread,
Apr 28, 2009, 2:53:22 PM4/28/09
to
Hi All

How to use weak_ptr to break cyclic reference here ?

#include <iostream>
#include <boost/smart_ptr.hpp>
using namespace std;

struct Dad;
struct Child;

typedef boost::shared_ptr<Dad> DadPtr;
typedef boost::shared_ptr<Child> ChildPtr;


struct Dad
{
ChildPtr myBoy;
};

struct CChild
{
DadPtr myDad;
};

// a "thing" that holds a smart pointer to another "thing":
DadPtr parent(new DadPtr);
ChildPtr child(new ChildPtr);

// deliberately create a circular reference:
parent->myBoy = child;
child->myDad = dad;

Thanks
Pallav Singh

Marcel Müller

unread,
Apr 28, 2009, 3:10:23 PM4/28/09
to
Hi,

Pallav singh wrote:
> // a "thing" that holds a smart pointer to another "thing":
> DadPtr parent(new DadPtr);
> ChildPtr child(new ChildPtr);
>
> // deliberately create a circular reference:
> parent->myBoy = child;
> child->myDad = dad;

If your child objects have an existence dependency on their parents you
can safely use a weak pointer from child to parent.


Marcel

0 new messages