class parent
{
public:
    ~parent() { std::cout <<"destroying parent\n"; }

public:
    children_ptr children;
};

class children
{
public:
    ~children() { std::cout <<"destroying children\n"; }

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

相关文章:

  • 2021-05-30
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-06
  • 2021-12-19
  • 2021-07-08
猜你喜欢
  • 2021-05-20
  • 2021-07-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-06
相关资源
相似解决方案