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