程序员

【c++进阶】智能指针的陷阱

2020-11-22  本文已影响0人  小鱼号的代码日记
/*
 * 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();
}

上一篇下一篇

猜你喜欢

热点阅读