全部书籍

专业

应用 关于 Coach 登录 开始阅读

书籍暂未推出简体中文版——当前显示英文版。

Computer Science · 术语表

什么是 Shared pointer, shared ownership, weak pointer?

又称: shared pointer · shared ownership · weak pointer

Definition 2.14 University Computer Science — Year 2 · 第 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.
在原文中阅读 →