如何将 std::shared_ptr 添加到多个 STL 容器?

Gas*_*sim 1 c++ stl smart-pointers c++11

将共享指针传递给不同对象中的 stl 容器的正确方法是什么,因此不会提前销毁对象?

我有多个带有 std::queue 的系统:

class System {
    typedef std::shared_ptr<Event> EventPtr;
protected:
    std::queue<EventPtr> mEventQueue;

    static SystemManager * sSystemManager;
   //this holds all the systems in the application

public:
    System();
    ~System();

    void addEventToQueue(EventPtr event) {
        mEventQueue.push(event);
    }

    void callEventQueue() {
        while(!mEventQueue.empty()) {
           acceptEvent(mEventQueue.front().get());
           mEventQueue.pop();
         }
    }

    void acceptEvent(Event * event);

public:
    static void sendEvent(EventPtr &event) {
        for(auto system : sSystemManager->getSystems()) {
               system->addEventToQueue(event);
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

我想知道我是否理解正确:

当我调用System::sendEvent(std::make_shared<Event>("testEvent"));作用域时,它将共享指针作为引用传递,该引用不会创建新指针并且不会增加引用计数。但是,addEventToQueue函数将参数作为对象传递,因此引用计数增加;如果我有 5 个系统,引用计数将为 6(计算 std::make_shared 本身)。但是这个引用计数存储在哪里呢?它是通过创建的第一个共享指针std::make_shared吗?还是所有对象的计数相同?那么,当第一个对象超出范围时,其他对象会发生什么?他们怎么知道正确的引用计数是多少,因为他们只知道“父”对象?

我读过的所有关于共享指针的文章,引用计数的显示方式总是很常见的。计数器是静态变量吗?

Rei*_*ica 5

正好在计数存储依赖于实现。但是,该标准规定它的行为必须使std::shared_ptr<T>共享一个实例所有权的所有实例T使用相同的引用计数。实际上,这意味着引用计数是动态分配的,并且指向它的指针由 的所有相关实例共享std::shared_ptr<T>

这就是std::make_shared()创建共享指针的首选方式的原因之一——它可以为引用计数(以及所需的其他维护结构)和一个分配请求中的对象分配内存,而不是两个单独的请求。这提高了分配的性能,也可能提高了指针的使用性能(因为引用计数和对象在内存中将更接近,从而降低缓存未命中的可能性)。

  • 注意:`make_shared` 有利也有弊。`std::make_shared&lt;BigObject&gt;` 是个坏主意,*如果*你打算在对象死后很久才使用 `std::weak_ptr&lt;BigObject&gt;`。 (2认同)