Tuesday, March 31, 2020 1:09 PM (about a few seconds ago)

Why we need smart pointer?

Manage heap memory manually is tedious.


include <memory>

For auto_ptr

template<class X> class auto_ptr {
  explicit auto_ptr(X* p = 0) throw ();

(The throw() notation, recall, means this constructor doesn’t throw an exception)

unique_ptr<double> pdu(new double); // pdu an unique_ptr to double
shared_ptr<string> pss(new string); // pss a shared_ptr to string