我试图了解以下代码中发生了什么。当object-a被删除时,它的shared_ptr成员变量object-b是否保留在内存中,因为object-c持有一个shared_ptr到object-b?
class B
{
public:
B(int val)
{
_val = val;
}
int _val;
};
class A
{
public:
A()
{
_b = new B(121);
}
boost::shared_ptr<B> _b;
};
class C
{
public:
C()
{
}
void setRef( boost::shared_ptr<B> b)
{
_b = b;
}
boost::shared_ptr<B> _b;
};
int main()
{
C c;
{
A *a = new A();
cout << "a._b.use_count: " << a->_b.use_count() << endl;
c.setRef(a->_b);
cout << "a._b.use_count: " << a->_b.use_count() << endl;
delete a;
}
cout << c._b->_val << endl;
}