class Wife;
class Husband
{
public:
void setWife(const shared_ptr<Wife>& wife)
{
this->myWife = wife;
cout << "myWife's use_count" << myWife.use_count() << endl;
}
~Husband()
{
cout << "myWife's use_count" << myWife.use_count() << endl;
cout << "Husband is deleted" << endl;
}
private:
shared_ptr<Wife> myWife;
};
class Wife
{
public:
void setHusband(const shared_ptr<Husband>& husband)
{
this->myHusband = husband;
cout << "myHusband's use_count" << myHusband.use_count() << endl;
}
~Wife()
{
cout << "myHusband's use_count" << myHusband.use_count() << endl;
cout << "Wife is deleted" << endl;
}
private:
weak_ptr<Husband> myHusband;
};
int main()
{
shared_ptr<Husband> husband(new Husband);
shared_ptr<Wife> wife(new Wife);
husband->setWife(wife);
wife->setHusband(husband);
}
|