- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我了解 shared_ptr 的工作原理,除了 weak_ptr 的作用。我知道当引用计数不为零时它会检测循环引用,但除此之外我不明白它是如何做到这一点的。它有什么作用?
最佳答案
另请参阅:When is std::weak_ptr useful?为什么和How does weak_ptr work?如何。
我将提供一个示例来说明我是如何使用它的,尽管我编写的示例代码有点令人费解,请耐心等待:
#include <vector>
#include <memory>
#include <ostream>
int main()
{
// Fill container with values 1-50. This container OWNS these values.
std::vector<std::shared_ptr<int>> owning_container;
for(int i = 1; i <= 50; ++i)
{
owning_container.emplace_back(std::make_shared<int>(i));
}
// Create a sepearate container that references all owned values that are multiples of 5.
std::vector<std::weak_ptr<int>> referencing_container;
for(std::shared_ptr<int> const& i : owning_container)
{
if((*i) % 5 == 0)
{
// Make weak_ptr that references shared_ptr
referencing_container.emplace_back(i);
}
}
// Go through the owned values and delete all that are multiples of 10.
for(auto it = owning_container.begin(); it != owning_container.end();)
{
std::shared_ptr<int> const& i = *it;
if(*i % 10 == 0)
{
it = owning_container.erase(it);
}
else
{
++it;
}
}
// Now go through the referencing container and print out all values.
// If we were dealing with raw pointers in both containers here we would access deleted memory,
// since some of the the actual resources (in owning_container) that referencing_container
// references have been removed.
for(std::weak_ptr<int> const& i_ref : referencing_container)
{
// Check if the shared resource still exists before using it (purpose of weak_ptr)
std::shared_ptr<int> i = i_ref.lock();
if(i)
{
std::cout << *i << std::endl;
}
}
return 0;
}
这里我们有一个容器,其中包含一些共享资源 - 在本例中为整数 - ( shared_ptr
),另一个容器需要引用 ( weak_ptr
)。引用不拥有资源,它只需要能够访问它如果它存在。为了判断被引用的资源是否仍然存在,我们将 weak_ptr
转换为到 shared_ptr
使用 weak_ptr::lock()
.那些仍然存在的资源将具有有效的 shared_ptr
由 lock()
返回.那些不再存在的将返回 null shared_ptr
. shared_ptr
有一个 operator bool()
我们可以在尝试使用它之前检查它是否为 null。
如果您制作的游戏中的每个对象都由 game_object
表示,那么情况可能不太复杂。 .假设您有某种寻找敌人的逻辑,需要目标 game_object
寻求。使用上面的方法,你可以让敌人捕获 weak_ptr<game_object>
.它不拥有这个 game_object
.如果其他东西杀死了它的目标,它的目标就应该死;如果敌人持有 shared_ptr
会发生这种情况,不会卡在某种边缘状态反而。这样如果敌人的目标还活着(它可以通过锁定 weak_ptr
来检查),它可以执行搜索逻辑;否则它可以找到新的目标。 game_object
的“所有者”可能是某种 game_world
类 - 这将有一个容器 shared_ptr<game_object>
.当敌人需要一个新目标时,它可以搜索这个容器并创建它的 weak_ptr
来自 game_world
的 shared_ptr
.
关于c++ - shared_ptr中weak_ptr的作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25223639/
我有以下数据结构: shared_ptr>> foo; 还有一个带有函数的渲染器类: void addObject(weak_ptr _obj) const 这个函数只是将_obj推回 mutable
我了解可用的方法以及它们是什么。请描述 weak_ptr 类的私有(private)部分或给出一些自定义 weak_ptr 代码的示例。我无法通过 std::weak_ptr 实现来理解。 最佳答案
我目前有类似以下内容: class Parent { //just a single child... for sake of simplicity //no other class
以下是 weak_ptr 的 2 个构造函数: http://msdn.microsoft.com/en-us/library/bb982126.aspx weak_ptr(const weak_pt
我有一个类将 weak_ptr 存储在一个容器中,如果 weak_ptr 没有过期,稍后会做一些事情: class Example { public: void fill(std::share
代码如下: struct lex_compare { bool operator() (const weak_ptr &lhs, const weak_ptr &rhs)const {
我想将原始指针成员包装到一些智能指针,以防止在开发类中删除。指针下对象的所有者在类之外。所以,看起来像 boost::shared_ptr 和 std::auto_ptr 不适合。以下是简化的示例:
考虑以下代码: #include #include using namespace std; struct MySharedStruct { int i; }; void print_valu
我了解如何使用 weak_ptr 和 shared_ptr。通过计算对象中的引用数,我了解 shared_ptr 的工作原理。 weak_ptr 是如何工作的?我尝试通读 boost 源代码,但我对
我目前正在为游戏设计一个对象结构,在我的例子中,最自然的组织变成了一棵树。作为智能指针的忠实粉丝,我只使用 shared_ptr 的。然而,在这种情况下,树中的子节点需要访问它的父节点(例如—— ma
根据 [util.smartptr.weak.obs]/4 ,weak_ptr::owner_before 严格弱排序是这样的:“两个 shared_ptr 或 weak_ptr 实例是等价的,当且仅
我正在做一个练习来了解共享指针和弱指针。 所以这个例子是shared_ptr的循环依赖,我该如何解决weak_ptr的问题。 我想将 root->left->parent 初始化为 root,对于右节
看看 std::weak_ptr 我在几个地方看到它可以用来打破由于使用 std::shared_ptr 的循环依赖导致的内存泄漏>。例如,请参见这两个已接受的答案:[1] , [2] . 采用最后引
在我的程序中,Groups 将共享指向 Subjects 的指针;并且 Subjects 将有指向其 Groups 的弱指针。我希望 Group 有一个 join() 函数,将 Subject 的弱指
假设我有一个包含子对象的 shared_ptr 的对象。 我希望子对象有一个指向父对象的 weak_ptr,子对象的构造函数应该是什么样子,我应该如何从父对象构造一个子对象? 提前致谢 最佳答案 由于
序言 在 C++11 中有 std::shared_ptr + std::weak_ptr 组合。尽管非常有用,但它有一个讨厌的问题:你 cannot easily construct shared_
我认为我的问题类似于 shared_ptr and weak_ptr differences ,但我感兴趣的是了解它们如何协同工作,而不是查看差异列表。 维基百科页面 shared_ptr and w
取消引用 weak_ptr 不会自动检查指针是否已过期。为什么要这样设计? 我想安全地取消对弱指针的引用,而不必首先使用 expired() 检查它们,如果它们为空则抛出异常。 这是一个好的做法吗?什
我正在尝试重写我们的 Observer/Observable 实现以使用 std::shared_ptr/std::weak_ptr 来摆脱代码中当前存在的一些讨厌的竞争条件。 通常,观察者会在满足某
我有一个带有自定义删除器的 boost::shared_ptr。将其转换为 weak_ptr 时删除者信息丢失了吗?如果是,我如何将相同的删除器重新附加到 shared_ptr - 从 weak_pt
我是一名优秀的程序员,十分优秀!