假设我有一个 Foo
的集合对象,每个 Foo
拥有一个或多个 Bar
对象。具体Foo
或 Bar
可以被我的界面的用户删除;当 Foo
被删除,所有Bar
它拥有的也被删除了。到目前为止,给每个 Foo
unique_ptr<Bar>
的集合这就是我自动管理这个模型所需要的。但是,我有一个额外的要求:一次 Foo
没有任何 Bar
,也应该删除。
当然,我可以只编写明确处理此问题的代码,但我想知道是否有更惯用的方法来实现同样的事情。这听起来很像 shared_ptr
, 但不完全...
最佳答案
自删除所有 Bar
来自 Foo
应该删除 Foo
, 听起来您确实需要一把 shared_ptr
来自 Bar
给他们的 Foo
.
但是,该模型会将 Foo
的生命周期置于在其手中Bar
s: 你不能直接删除 Foo
,相反,您必须找到它的所有 Bar
s 并删除这些。
Foo
会保留 Bar*
s 而不是 unique_ptr<Bar>
s,因为它不能在其 Bar
之前死亡秒。但是你必须提供 Bar
的所有权给某人...
您最终可能会得到另一个包含 unique_ptr<Bar>
集合的对象s对应于每个Foo
s,但是你必须保持所有这些同步为 Bar
来来去去。这与您试图避免的簿记类型相同,但结果更大、更复杂、更脆弱,内存泄漏和流氓指针是失败案例。
因此,我建议您不要使用所有这些,而是坚持使用第一个 unique_ptr
动力的想法。第一次实现可能如下所示:
struct Foo {
private:
friend void remove(std::unique_ptr<Foo> &foo, Bar const *bar);
// Removes the bar from this Foo.
// Returns true iff the Foo is now empty and should be deleted.
bool remove(Bar const *bar) {
auto i = std::find_if(begin(_bars), end(_bars), [&](auto const &p) {
return p.get() == bar;
});
assert(i != end(_bars));
_bars.erase(i);
return _bars.empty();
}
std::vector<std::unique_ptr<Bar>> _bars;
};
// Removes the bar from the Foo.
// Deletes the Foo if it becomes empty.
void remove(std::unique_ptr<Foo> &foo, Bar const *bar) {
if(foo->remove(bar))
foo.reset();
}
关于c++ - 如何优雅地使用智能指针在 C++ 中为复杂的生命周期建模?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40460922/