/*
* c++进阶
* 智能指针的陷阱
* 小鱼号的代码日志
*/
#include <QCoreApplication>
#include<iostream>
#include<memory>
using namespace std;
class Parent;
typedef shared_ptr<Parent> ParentPtr;
class Child
{
public:
ParentPtr father;
Child();
~Child();
};
typedef shared_ptr<Child> ChildPtr;
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();
}
网友评论