循环引用
class Child;
class Parent
{
public:
my_weak_ptr<Child> c;
public:
Parent() { cout << "Parent" << endl; }
~Parent() { cout << "~Parent" << endl; }
void hi()const { cout << "hello parent" << endl; }
};
class Child
{
public:
my_weak_ptr<Parent> p;
Child() { cout << "Child" << endl; }
~Child() { cout << "~Child" << endl; }
};
void fun()
{
my_shared_ptr<Parent> parent(new Parent());
my_shared_ptr<Child> child(new Child());
parent->c = child;
child->p = parent;
child->p.lock()->hi();
}