How do shared pointers work?

前端 未结 5 1838
无人共我
无人共我 2020-12-02 13:34

How do shared pointers know how many pointers point to that object? (shared_ptr, in this case)

5条回答
  •  再見小時候
    2020-12-02 13:50

    They hold an internal reference count that is incremented in the shared_ptr copy constructor/assignment operator and decremented in the destructor. When the count reaches zero, the held pointer is deleted.

    Here's the Boost library documentation for smart pointers. I think the TR1 implementation is mostly the same as boost::shared_ptr.

提交回复
热议问题