美文网首页程序员
【c++进阶】智能指针weak_ptr

【c++进阶】智能指针weak_ptr

作者: 小鱼号的代码日记 | 来源:发表于2021-02-02 22:38 被阅读0次
    /*
     * 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();
    }
    
    

    相关文章

      网友评论

        本文标题:【c++进阶】智能指针weak_ptr

        本文链接:https://www.haomeiwen.com/subject/zfkpiktx.html