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

后端 未结 2 1356
温柔的废话
温柔的废话 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.)

    0 讨论(0)
  • 2021-02-06 05:17

    You have not leaked memory. The memory for the first int object will be deleted.

    0 讨论(0)
提交回复
热议问题