shared_ptr & weak_ptr conversions

前端 未结 3 1329
攒了一身酷
攒了一身酷 2021-02-07 08:54

I am trying to juggle objects using std::shared_ptr and std::weak_ptr. The scenario is something like this:

I have objects of class chann

3条回答
  •  情书的邮戳
    2021-02-07 09:25

    You cannot

    convert a reference to an object to a weak pointer

    You can make a weak pointer from a shared pointer, just using assignment = e.g.

    std::shared_ptr get_free_channel();
    

    then

    bool release_channel(std::shared_ptr ch)
    {
        std::weak_ptr weak_ch = ch;
        //...
    }
    

    Watch out for lifetimes - will the shared_ptr go before the weak pointers that point to them?

提交回复
热议问题