Set shared_ptr to point existing object

前端 未结 2 445
终归单人心
终归单人心 2021-01-04 20:18

For the code below, I would like to know how to set std::shared_ptr to point the given objects in the two member functions. The Vector3 object whic

2条回答
  •  灰色年华
    2021-01-04 20:33

    Disregarding the argument about whether using shared_ptr is a good idea or not, as explained by the accepted answer, you can use the following if you continue to use shared_ptr:

    void addVtx (const Vector3& vt)
    {
        vtx.push_back(std::make_shared(vt));
    }
    
    void setVtx (size_t v, const Vector3& vt)
    {
        if ( vtx.size() > 0 && v < vtx.size() )
        {
           vtx[v] = std::make_shared(vt);
        }
    }
    

提交回复
热议问题