Tutorial by Examples

The class template std::shared_ptr defines a shared pointer that is able to share ownership of an object with other shared pointers. This contrasts to std::unique_ptr which represents exclusive ownership. The sharing behavior is implemented through a technique known as reference counting, where the...
Instances of std::weak_ptr can point to objects owned by instances of std::shared_ptr while only becoming temporary owners themselves. This means that weak pointers do not alter the object's reference count and therefore do not prevent an object's deletion if all of the object's shared pointers are ...
C++11 A std::unique_ptr is a class template that manages the lifetime of a dynamically stored object. Unlike for std::shared_ptr, the dynamic object is owned by only one instance of a std::unique_ptr at any time, // Creates a dynamic int with value of 20 owned by a unique pointer std::unique_pt...
Many C interfaces such as SDL2 have their own deletion functions. This means that you cannot use smart pointers directly: std::unique_ptr<SDL_Surface> a; // won't work, UNSAFE! Instead, you need to define your own deleter. The examples here use the SDL_Surface structure which should be fre...
C++11 NOTE: std::auto_ptr has been deprecated in C++11 and will be removed in C++17. You should only use this if you are forced to use C++03 or earlier and are willing to be careful. It is recommended to move to unique_ptr in combination with std::move to replace std::auto_ptr behavior. Before we ...
enable_shared_from_this enables you to get a valid shared_ptr instance to this. By deriving your class from the class template enable_shared_from_this, you inherit a method shared_from_this that returns a shared_ptr instance to this. Note that the object must be created as a shared_ptr in first pl...
It is not possible to directly use static_cast, const_cast, dynamic_cast and reinterpret_cast on std::shared_ptr to retrieve a pointer sharing ownership with the pointer being passed as argument. Instead, the functions std::static_pointer_cast, std::const_pointer_cast, std::dynamic_pointer_cast and ...
A value_ptr is a smart pointer that behaves like a value. When copied, it copies its contents. When created, it creates its contents. // Like std::default_delete: template<class T> struct default_copier { // a copier must handle a null T const* in and return null: T* operator()(T co...

Page 1 of 1