std::shared_ptr deep copy object

前端 未结 1 440
被撕碎了的回忆
被撕碎了的回忆 2020-12-31 08:10

Can\'t find much on that for C++11 but only on boost.

Consider the following class:

class State
{
   std::shared_ptr _graph;

 public:

         


        
1条回答
  •  隐瞒了意图╮
    2020-12-31 08:23

    If you want to make a copy of the Graph object when you make a copy of the object, you can always define your copy constructor and assignment operator to do just that:

    State::State(const State& rhs) : _graph(std::make_shared(*rhs._graph)) {
       // Handled by initializer list
    }
    State::State(State&& rhs) : _graph(std::move(rhs._graph)) {
       // Handled by initializer list
    }
    State& State::operator= (State rhs) {
        std::swap(*this, rhs);
        return *this;
    }
    

    Hope this helps!

    0 讨论(0)
提交回复
热议问题