| Start |
|---|
| Unique ownership (std::unique_ptr) A (Note: Only the variable To use You can access the You can transfer ownership of the contents of a smart pointer to another pointer by using Passing Returning Compare this to: The class template Unlike the dumb smart pointer ( Which can be simplified with: Now, you use You need not to worry about de-allocation. This template specialized version calls constructors and destructors appropriately. Using vectored version of In versions prior to C++11, |
| Sharing ownership (std::shared_ptr) The class template The sharing behavior is implemented through a technique known as reference counting, where the number of shared pointers that point to the object is stored alongside it. When this count reaches zero, either through the destruction or reassignment of the last To create multiple smart pointers that share the same object, we need to create another Either of the above ways makes The smart pointer works just like a raw pointer. This means, you can use Finally, when the last aliased Warning: Constructing a Allocating Arrays([]) using shared_ptr Unfortunately, there is no direct way to allocate Arrays using It is possible to create arrays for For example, to allocate an array of 10 integers, we can write the code as Specifying If we know the size at compile time, we can do it this way: then With C++17, Shared pointers can point to a sub-object of the object it owns: Both Important: A Ownership Transfer of shared_ptr By default, |
| Sharing with temporary ownership (std::weak_ptr) Instances of In the following example instances of As child nodes are added to the root node's children, their Due to control block implementation details, shared_ptr allocated memory may not be released until Since |
| Using custom deleters to create a wrapper to a C interface Many C interfaces such as SDL2 (opens new window) have their own deletion functions. This means that you cannot use smart pointers directly: Instead, you need to define your own deleter. The examples here use the The deleter must be callable with a pointer argument, and therefore can be e.g. a simple function pointer: Any other callable object will work, too, for example a class with an This not only provides you with safe, zero overhead (if you use Note that the deleter is part of the type for In cases where reference counting is important, one could use a With With which the above example is simply: Here, the purpose of |
| Unique ownership without move semantics (auto_ptr) NOTE: Before we had As with all smart pointers, but allows only one owner: This allows to use std::auto_ptr to keep ownership explicit and unique at the danger of losing ownership unintended: The transfer of ownership happened in the "copy" constructor. This breaks copy semantics, which require that copying an object leaves you with two equivalent versions of it. For any copyable type, But for |
| Casting std::shared_ptr pointers It is not possible to directly use Note that |
| Writing a smart pointer: value_ptr A This particular value_ptr is only empty if you construct it with This smart pointer can be useful for With a non-default |
| Getting a shared_ptr referring to this
By deriving your class from the class template Note that the object must be created as a Note(2) you cannot call If you use Using Syntax
RemarksC++ is not a memory-managed language. Dynamically allocated memory (i.e. objects created with Smart pointers can be used to automatically manage the scope of dynamically allocated memory (i.e. when the last pointer reference goes out of scope it is deleted). Smart pointers are preferred over "raw" pointers in most cases. They make the ownership semantics of dynamically allocated memory explicit, by communicating in their names whether an object is intended to be shared or uniquely owned. Use |
Read Article
https://develop.horje.com/learn/1434/reference