I learn smart pointers. And right now I am learning reference loop. I found some code in net and changed it, but it doesn't work. Please help. Function .lock() does nothing
This doesn't really do anything because _B and _A->b are already pointing to the same object. Only one shared_ptr points to the object so use_count() returns 1 as expected.
lock() returns a temporary shared_ptr object. When this object is created the use count is increased by 1 because there is now one more shared_ptr pointing to the object.
The temporary shared_ptr object is then assigned to _B. The use count of the object pointed to by _B before the assignment will be decreased by 1 (if it drops to 0 it will be deleted) and then the use count of the assigned object will be increased by 1. In this case _B points to the same object before and after so the use count stays the same (-1+1=0).
At the end of the line the temporary shared_ptr object is destroyed so the use count is decreased by 1.