std::shared_ptr deep copy object

Ælex picture Ælex · Nov 14, 2013 · Viewed 14k times · Source

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

Consider the following class:

class State
{
   std::shared_ptr<Graph> _graph;

 public:

    State( const State & state )
    {
        // This is assignment, and thus points to same object
        this->_graph = std::make_shared<Graph>( state._graph ); 

        // Deep copy state._graph to this->_graph ?
        this->_graph = std::shared_ptr<Graph>( new Graph( *( state._graph.get() ) ) );

        // Or use make_shared?
        this->_graph = std::make_shared<Graph>( Graph( *( state._graph.get() ) ) );
    }   
};

Suppose class Graph does have a copy constructor:

Graph( const Graph & graph )

I do not want to have this->_graph point/share the same object! Instead, I want this->_graph to deep copy the object from state._graph, into my own this->_graph duplicate.

Is the way above the correct way of going about this?

Documentation of std::make_shared notes that:

Moreover, f(shared_ptr(new int(42)), g()) can lead to memory leak if g throws an exception. This problem doesn't exist if make_shared is used.

Is there another way of going about this, safer or more reliable?

Answer

templatetypedef picture templatetypedef · Nov 14, 2013

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!