samedi 16 février 2019

Any use for dynamically allocating std::unique_ptr?

Does it ever make sense to create a std::unique_ptr using new? In the snippet below as I suspected the SimpleClass object being managed by the std::unique_ptr won't be destroyed unless I delete the std::unique_ptr myself. I couldn't think of a situation where this is useful so I was wondering if there are situations where this is actually used.

  std::unique_ptr<vector_test::SimpleClass>* ptr_to_unique_ptr = new std::unique_ptr<vector_test::SimpleClass>();
  ptr_to_unique_ptr->reset(new vector_test::SimpleClass(555));
  delete ptr_to_unique_ptr;

Aucun commentaire:

Enregistrer un commentaire