/*
* c++进阶
* 智能指针
* weak_ptr
* 小鱼号的代码日志
*/
#include <QCoreApplication>
#include<iostream>
#include<memory>
using namespace std;
class Parent;
typedef shared_ptr<Parent> ParentPtr;
typedef weak_ptr<Parent> WeakParentPtr;
class Child
{
public:
WeakParentPtr father;
Child();
~Child();
};
typedef shared_ptr<Child> ChildPtr;
typedef weak_ptr<Child> WeakChildPtr;
class Parent
{
public:
ChildPtr son;
Parent();
~Parent();
};
Parent::Parent()
{
cout << "new parent" << endl;
}
Child::Child()
{
cout << "new Child" << endl;
}
Parent::~Parent()
{
cout << "bye parent" << endl;
}
Child::~Child()
{
cout << "bye Child" << endl;
}
void testParentAndChild()
{
ParentPtr p(new Parent());
ChildPtr c(new Child());
p->son = c;
c->father = p;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
testParentAndChild();
return a.exec();
}
网友评论