c++ - 如何优雅地使用智能指针在 C++ 中为复杂的生命周期建模?

标签 c++ smart-pointers raii object-lifetime

假设我有一个 Foo 的集合对象,每个 Foo拥有一个或多个 Bar对象。具体FooBar可以被我的界面的用户删除;当 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/

相关文章:

c++ - 如何确保(像最终尝试一样)销毁 HEAP-ALLOCATED 对象

c++ - 等待多个线程通知的条件变量的正确方法

java - Java 中的智能指针和引用计数

C++11 numeric_limits<long>::min() 的abs();

c++ - 为什么 `this`等于0x0,导致我的程序崩溃?

boost shared_ptr 循环引用?

c++ - RAII 捕获构造函数异常的方法

c++ - 使用 RAII 从 C 风格的 API 管理资源

c++ - C++ 静态多态性和方法名称

c++ - 检测循环依赖的依赖排序