How to properly duplicate an object given its shared_ptr

std::make_shared is just a simple template function that creates the objects, passing all arguments to the constructor :

template<class T, class... Args>
shared_ptr<T> make_shared(Args&&... args)
{
  return shared_ptr<T>( new T( std::forward<Args>( args )... ) );
}

In your particular case :

std::shared_ptr<Event> o = std::make_shared<Event>(*e);

the object is copied.

If your code is such :

void foo() {
    // create new object using default constructor
    std::shared_ptr<Event> e = std::make_shared<Event>();
    // create new object using copy constructor constructor
    std::shared_ptr<Event> o = std::make_shared<Event>(*e);
}

then of course both objects are destroyed, when they go out of scope.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)