title: Multiple instances of a unique_ptr
body: <p>After discovering that I needed to <code>delete</code> any <code>new</code> pointers I create, I quickly realized that my project was full of memory leaks and I didn't even know it. So I was prompted to use smart pointers. However I have an issue when trying to create multiple instances of a smart pointer. I've created an SSCE to better explain this.</p>
  
  <p>mainwindow.h</p>
  
  <pre><code>#ifndef MAINWINDOW_H
  #define MAINWINDOW_H
  
  #include &lt;QMainWindow&gt;
  #include &lt;memory&gt;
  #include "classa.h"
  
  namespace Ui {
  class MainWindow;
  }
  
  class MainWindow : public QMainWindow
  {
      Q_OBJECT
  
  public:
      explicit MainWindow(QWidget *parent = 0);
      ~MainWindow();
  
  private slots:
      void on_pushButton_clicked();
  
  private:
      Ui::MainWindow *ui;
      std::unique_ptr&lt;ClassA&gt; classa; //&lt;----- a smart pointer of a Class type
  };
  
  #endif // MAINWINDOW_H
  </code></pre>
  
  <p>mainwindow.cpp</p>
  
  <pre><code>#include "mainwindow.h"
  #include "ui_mainwindow.h"
  #include "classa.h"
  #include &lt;QDebug&gt;
  
  QVector&lt;ClassA*&gt; classes;//&lt;------ QVector that contains instances of ClassA
                                      //So I can retrieve them later based on an index.
  
  MainWindow::MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
  {
      ui-&gt;setupUi(this);
      classa = std::unique_ptr&lt;ClassA&gt;(new ClassA()); //&lt;---- Created here
      classes.push_back(classa.get()); //&lt;---- appended to the QVector
  }
  
  MainWindow::~MainWindow()
  {
      delete ui;
  }
  
  //When the button is clicked, the program crashes when trying to qDebug
  //because the first instance that was loaded in the QVector is no longer
  //"valid." It stops being "valid" when I create the second instance of ClassA
  void MainWindow::on_pushButton_clicked()
  {
      classa = std::unique_ptr&lt;ClassA&gt;(new ClassA());
      classes.push_back(classa.get());
  
      qDebug() &lt;&lt; classes.at(0);
  }
  </code></pre>
  
  <p>Hopefully the above comments explain enough. The first solution that came to mind was to change my declaration in mainwindow.h to <code>std::unique_ptr&lt;ClassA&gt; *classa;</code> but wouldn't that defeat the purpose of the smart pointer?</p>
  
  <p>Really confused. Thanks for your time.</p>