Create a boost::shared_ptr to an existing variable

前端 未结 4 1183
星月不相逢
星月不相逢 2020-12-08 07:32

I have an existing variable, e.g.

int a = 3;

How can I now create a boost::shared_ptr to a? For example:

4条回答
  •  被撕碎了的回忆
    2020-12-08 08:05

    You cannot create a boost::shared_ptr for an existing variable. Items stored in a boost::shared_ptr are stored at creation.

    You can however make a boost::shared_ptr that is a copy of an existing variable.

    For example

    int a = 3; // Existing variable
    boost::shared_ptr aCopy = boost::make_shared(a); //Create copy with value of a
    

    Note that you will need to include for make_shared.

提交回复
热议问题