question on assignment with boost::shared_ptr (vs. the reset() function)

后端 未结 2 1355
温柔的废话
温柔的废话 2021-02-06 04:43

Sorry if this is explicitly answered somewhere, but I\'m a little confused by the boost documentation and articles I\'ve read online.

I see that I can use the reset() fu

2条回答
  •  别跟我提以往
    2021-02-06 04:57

    The documentation is fairly clear:

    shared_ptr & operator=(shared_ptr const & r); // never throws

    Effects: Equivalent to shared_ptr(r).swap(*this).

    So it just swaps ownership with the temporary object you create. The temporary then expires, decreasing the reference count. (And deallocating if zero.)


    The purpose of these containers is to not leak memory. So no, you don't need to worry about leaking things unless you're trying to mess things up on purpose. (In other words, you probably don't need to doubt Boost knows what they're doing.)

提交回复
热议问题