title: "Recreating" an object in C++ does not reset the values of attributes. (Qt)
body: <p>I think it would be easier to just put the code down, but what I want is to "recreate" an object.
  So in the simple example I have a class with an int as attribute. When I recreate the class using
  <code>object = new Object();</code>, the int keep the same value, it is not reset.</p>
  
  <p>Here is the code of the simple problem (it looks overly complicated, but there is a reason for that):</p>
  
  <p>main.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "main.h"
  
  using namespace std;
  
  int main(int argc, const char * argv[]) {
      Object *object = new Object();
      object-&gt;setNumber(4);
      cout &lt;&lt; endl &lt;&lt; object-&gt;getNumber()&lt;&lt;endl;
      restart(object);
      cout &lt;&lt; endl &lt;&lt; object-&gt;getNumber()&lt;&lt;endl;
      return 0;
  }
  
  void restart(Object *object)
  {
      object = new Object();
  }
  </code></pre>
  
  <p>main.h</p>
  
  <pre><code>#ifndef test_main_h
  #define test_main_h
  #include "object.h"
  
  void restart(Object *object);
  
  #endif
  </code></pre>
  
  <p>object.cpp</p>
  
  <pre><code>using namespace std;
  
  Object::Object()
  {
      m_number = new int(2);
  }
  
  void Object::setNumber(int number)
  {
      m_number = new int(number);
  }
  
  int Object::getNumber()
  {
      return *m_number;
  }
  </code></pre>
  
  <p>object.h</p>
  
  <pre><code>#ifndef __test__object__
  #define __test__object__
  
  #include &lt;stdio.h&gt;
  
  class Object
  {
  public:
      Object();
      void setNumber(int number);
      int getNumber();
  
  private:
      int *m_number;
  };
  
  #endif /* defined(__test__object__) */
  </code></pre>
  
  <p>And so the output would be:</p>
  
  <pre><code>4
  
  4
  Program ended with exit code: 0
  </code></pre>
  
  <p>We would want 2 at the last output.</p>
  
  <p>Now if you want to know why I did all this, it's because I'm making a program with Qt, and I have the same problem, but a bit more complicated:</p>
  
  <p>main.cpp</p>
  
  <pre><code>#include "mainwindow.h"
  #include &lt;QApplication&gt;
  
   int main(int argc, char *argv[])
  {
      QApplication a(argc, argv);
      Main main;
      main.show();
      return a.exec();
  }
  </code></pre>
  
  <p>mainwindow.h</p>
  
  <pre><code>#ifndef LESSON_H
  #define LESSON_H
  
  #include &lt;QAudioProbe&gt;
  #include &lt;QAudioRecorder&gt;
  #include &lt;QDir&gt;
  #include &lt;QFileDialog&gt;
  #include &lt;QMediaRecorder&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;QVariant&gt;
  #include &lt;QDir&gt;
  #include &lt;QtCore&gt;
  #include &lt;QtWidgets&gt;
  #include &lt;QMediaPlayer&gt;
  #include &lt;QKeyEvent&gt;
  
   class MainWindow : public QWidget
  {
      Q_OBJECT
  
   public:
      MainWindow();
  
   private slots:
      void togglePause();
      void toggleRecord();
      void setEnabled();
  
   signals:
      void end();
  
   private:
      QPushButton *m_record;
      QPushButton *m_recordPause;
      QPushButton *m_enableB;
      bool m_enable;
      QLabel *m_recordState;
      QMediaPlayer *m_audio;
      QAudioRecorder *m_audioRecorder;
      QString m_dossier;
  };
  
   class Main : public QWidget
  {
      Q_OBJECT
  
   public:
      Main();
   private slots:
      void restart();
  
   private:
      MainWindow *main;
  };
  
  #endif // LESSON_H
  </code></pre>
  
  <p>mainwindow.cpp</p>
  
  <pre><code>#include "mainwindow.h"
  
   using namespace std;
  
   MainWindow::MainWindow()
  {
  
      m_record = new QPushButton("Record");
      m_recordPause = new QPushButton("Pause");
      m_recordPause-&gt;setEnabled(false);
      m_enableB = new QPushButton("Paste enabled");
      m_recordState = new QLabel("Recording paused.");
      m_recordState-&gt;setAlignment(Qt::AlignCenter);
  
      m_enable = true;
  
      QGridLayout *layout = new QGridLayout;
      layout-&gt;addWidget(m_record,0,0,1,1);
      layout-&gt;addWidget(m_recordPause,1,0,1,1);
      layout-&gt;addWidget(m_enableB,2,0,1,1);
      layout-&gt;addWidget(m_recordState,3,0,1,1);
      setLayout(layout);
  
       m_dossier = QFileDialog::getSaveFileName(this, "Save Audio File To:", QString(), "Audio (*.wav)");
  
       m_audioRecorder = new QAudioRecorder(this);
       m_audioRecorder-&gt;setOutputLocation(QUrl::fromLocalFile(m_dossier));
       m_audioRecorder-&gt;setAudioInput(m_audioRecorder-&gt;defaultAudioInput());
  
       QAudioEncoderSettings settings;
       settings.setCodec("audio/amr");
       settings.setSampleRate(16000);
       settings.setBitRate(32);
       settings.setQuality(QMultimedia::HighQuality);
       settings.setEncodingMode(QMultimedia::ConstantQualityEncoding);
  
       m_audioRecorder-&gt;setEncodingSettings(settings);
  
       m_audio = new QMediaPlayer;
       m_audio-&gt;setMedia(QUrl::fromLocalFile(m_dossier));
  
       connect(m_recordPause, SIGNAL(clicked()), this, SLOT(togglePause()));
       connect(m_record, SIGNAL(clicked()), this, SLOT(toggleRecord()));
       connect(m_enableB, SIGNAL(clicked()), this, SLOT(setEnabled()));
  }
  
  void MainWindow::toggleRecord()
  {
       if (m_audioRecorder-&gt;state() == QMediaRecorder::StoppedState) {
          m_record-&gt;setText("Stop");
          m_recordPause-&gt;setEnabled(true);
          m_audioRecorder-&gt;record();
          m_recordState-&gt;setText("Recording.");
      }
       else
      {
          m_audioRecorder-&gt;stop();
          m_audio-&gt;setMedia(QUrl::fromLocalFile(m_dossier));
          emit end();
      }
  }
  
        void MainWindow::togglePause()
  {
       if (m_audioRecorder-&gt;state() != QMediaRecorder::PausedState)
      {
          m_audioRecorder-&gt;pause();
          m_recordPause-&gt;setText("Resume");
          m_recordState-&gt;setText("Recording paused.");
      }
      else
      {
          m_audioRecorder-&gt;record();
          m_recordPause-&gt;setText("Pause");
          m_recordState-&gt;setText("Recording.");
      }
  }
  
       void MainWindow::setEnabled()
  {
      if(m_enable)
      {
          m_enableB-&gt;setText("Paste disabled");
          m_enable = false;
      }
      else
      {
          m_enableB-&gt;setText("Paste enable");
          m_enable = true;
      }
  }
  
   Main::Main()
  {
      main = new MainWindow();
      QGridLayout *layout = new QGridLayout;
      layout-&gt;addWidget(main,0,0,1,1);
      setLayout(layout);
      this-&gt;setFixedSize(200,200);
      this-&gt;setWindowTitle("Notes");
      connect(main, SIGNAL(end()), this, SLOT(restart()));
  }
  
   void Main::restart()
  {
      main = new MainWindow();
  }
  </code></pre>
  
  <p>Thank you very much for your help.</p>