8 Matching Annotations
  1. Apr 2022
    1. std::shared_ptr can be used when you need multiple smart pointers that can co-own a resource. The resource will be deallocated when the last std::shared_ptr goes out of scope. std::weak_ptr can be used when you want a smart pointer that can see and use a shared resource, but does not participate in the ownership of that resource.

      weak_ptr 的适用场景

    1. Always make a copy of an existing std::shared_ptr if you need more than one std::shared_ptr pointing to the same resource.

      如果要创建多个 shared_ptr,推荐的做法是什么?

    1. Second, don’t manually delete the resource out from underneath the std::unique_ptr.

      有什么误用 std::unique_ptr 的情况?

    2. Use std::make_unique() instead of creating std::unique_ptr and using new yourself.

      推荐的创建 std::unique_ptr 的方式是什么?有什么好处?

    3. Favor std::array, std::vector, or std::string over a smart pointer managing a fixed array, dynamic array, or C-style string.

      对于固定的 array,动态 array 和字符串,更推荐使用哪种类型?

    4. Because std::unique_ptr is designed with move semantics in mind, copy initialization and copy assignment are disabled. If you want to transfer the contents managed by std::unique_ptr, you must use move semantics.

      std::unique_ptr 可以使用 copy 初始化吗?

  2. Mar 2022
    1. A Smart pointer is a composition class that is designed to manage dynamically allocated memory and ensure that memory gets deleted when the smart pointer object goes out of scope.

      smart pointer 是什么?有什么好处?

  3. Nov 2021
    1. One of the best things about classes is that they contain destructors that automatically get executed when an object of the class goes out of scope. So if you allocate (or acquire) memory in your constructor, you can deallocate it in your destructor, and be guaranteed that the memory will be deallocated when the class object is destroyed (regardless of whether it goes out of scope, gets explicitly deleted, etc…).

      smart pointer 的原理是什么?