Tous les livres

Professionnel

Applis À propos Coach Connexion Commencer la lecture

Computer Science · Glossaire

Qu'est-ce que « Shared pointer, shared ownership, weak pointer » ?

Aussi appelé : shared pointer · shared ownership · weak pointer

Definition 2.14 University Computer Science — Year 2 · Chapitre 2 — Copying, Moving and Smart Pointers

A shared pointer std::shared_ptr<T> is one of possibly several owners of an object (shared ownership). The object comes with a control block that counts its shared pointers: copying a shared_ptr adds one, destroying or resetting one subtracts one, and the owner that brings the count to zero deletes the object. A weak pointer std::weak_ptr<T> refers to an object owned by shared pointers without being counted as an owner; lock() gives a shared_ptr to the object if it is still alive, and a null one if not.

Each object made by make_shared carries a control block counting its strong owners and its weak observers. Left: the cycle of  once a and p are gone. Right: inside the block of , while a and p still exist; the dashed edge does not count, so the album dies with a.
Each object made by make_shared carries a control block counting its strong owners and its weak observers. Left: the cycle of Listing 2.9 once a and p are gone. Right: inside the block of Listing 2.10, while a and p still exist; the dashed edge does not count, so the album dies with a.
Lire dans le chapitre →