循环引用:
引用计数是一种便利的内存管理机制,但它有一个很大的缺点,那就是不能管理循环引用的对象。一个简单的例子如下:
#include<string>
#include<iostream>
#include<boost/shared_ptr.hpp>
#include<boost/weak_ptr.hpp>
classparent;
classchildren;
typedefboost::shared_ptr<parent>
parent_ptr;
typedefboost::shared_ptr<children>
children_ptr;
classparent
{
public:
~parent() { std::cout <<"destroying
parent\n"; }
public:
children_ptr children;
};
classch......
阅读全文