zoukankan      html  css  js  c++  java
  • Shared_ptr 相互引用问题

    class parent
    {
    public:
        ~parent() { std::cout <<"destroying parent
    "; }
    
    public:
        children_ptr children;
    };
    
    class children
    {
    public:
        ~children() { std::cout <<"destroying children
    "; }
    
    public:
        parent_ptr parent;
    };
    
    
    void test()
    {
        parent_ptr father(new parent());
        children_ptr son(new children);
    
        father->children = son;
        son->parent = father;
    }
    

    在函数 test() 中,father->children = son, 使得 father 的计数器加至 2, son 同理,以至于析构 father(或son)时,仅仅是计数器减一,指针指向的资源并没有被析构。

    参考

    [1] http://blog.csdn.net/liuzhi1218/article/details/6993135

  • 相关阅读:
    2019 Java 第四周总结
    2019第三周总结
    Java 第二周总结
    2019春第十二周作业
    Day3
    Day3
    Day3
    Day3
    Day2
    Day2
  • 原文地址:https://www.cnblogs.com/zhouzhuo/p/3761975.html
Copyright © 2011-2022 走看看