????????C++?????????????????????????????????????????塣???????????????????????????????????????????????????????????й?????????????????????ν?????????????????????

????????????????????Boost??????C++??????????VS2005??????????Win32????

??????????????

#include "stdafx.h"
#include <string>
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>

using namespace std;
using namespace boost;

class CCycleRef
{
public:
    ~CCycleRef()
    {
        cout <<"destroying CCycleRef"<<endl;
    }

public:
    shared_ptr<CCycleRef> selfRef;
};

void CycleRefTest()
{
    shared_ptr<CCycleRef> cyclRef(new CCycleRef());
    cyclRef->selfRef = cyclRef;

    cout<<"reference count:"<<cyclRef.use_count()<<endl;
}

int _tmain(int argc?? _TCHAR* argv[])
{
    CycleRefTest();
  
    return 0;
}

???????н????

????reference count:2

??????????CCycleRef????????????

?????????CCycleRef?????????????????ü??????????£?????????

??????????y??

????????weak_ptr????????????????????????weak_ptr??????????ü?????

???????CCycleRef??

class CCycleRef
{
public:
    ~CCycleRef()
    {
        cout <<"destroying CCycleRef"<<endl;
    }

public:
    weak_ptr<CCycleRef> selfRef;
};

???????н??

????reference count:1

????destroying CCycleRef

??????????CCycleRef???????????