Yes, you have misinterpreted this. In your example, all the pointers are pointing to the same object, not forming any cycles.
The assignment of p4 to p2 is a no-op, since those pointers were already equal to begin with.
Here's an example with real cyclic references, maybe that will clear things up:
struct A
{
std::shared_ptr<A> ptr;
};
void main()
{
std::shared_ptr<A> x=std::make_shared<A>();
std::shared_ptr<A> y=std::make_shared<A>();
x->ptr = y; // not quite a cycle yet
y->ptr = x; // now we got a cycle x keeps y alive and y keeps x alive
}
You can even make this even simpler:
void main()
{
std::shared_ptr<A> x=std::make_shared<A>();
x->ptr = x; // never die! x keeps itself alive
}
In both examples, the objects in the shared_ptrs are never destructed, even after you leave main.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…