Q:

"Recreating" an object in C++ does not reset the values of attributes. (Qt)

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
object = new Object();, the int keep the same value, it is not reset.
Here is the code of the simple problem (it looks overly complicated, but there is a reason for that):
main.cpp
#include <iostream>
#include "main.h"

using namespace std;

int main(int argc, const char * argv[]) {
    Object *object = new Object();
    object->setNumber(4);
    cout << endl << object->getNumber()<<endl;
    restart(object);
    cout << endl << object->getNumber()<<endl;
    return 0;
}

void restart(Object *object)
{
    object = new Object();
}

main.h
#ifndef test_main_h
#define test_main_h
#include "object.h"

void restart(Object *object);

#endif

object.cpp
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;
}

object.h
#ifndef __test__object__
#define __test__object__

#include <stdio.h>

class Object
{
public:
    Object();
    void setNumber(int number);
    int getNumber();

private:
    int *m_number;
};

#endif /* defined(__test__object__) */

And so the output would be:
4

4
Program ended with exit code: 0

We would want 2 at the last output.
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:
main.cpp
#include "mainwindow.h"
#include <QApplication>

 int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Main main;
    main.show();
    return a.exec();
}

mainwindow.h
#ifndef LESSON_H
#define LESSON_H

#include <QAudioProbe>
#include <QAudioRecorder>
#include <QDir>
#include <QFileDialog>
#include <QMediaRecorder>
#include <fstream>
#include <string>
#include <QVariant>
#include <QDir>
#include <QtCore>
#include <QtWidgets>
#include <QMediaPlayer>
#include <QKeyEvent>

 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

mainwindow.cpp
#include "mainwindow.h"

 using namespace std;

 MainWindow::MainWindow()
{

    m_record = new QPushButton("Record");
    m_recordPause = new QPushButton("Pause");
    m_recordPause->setEnabled(false);
    m_enableB = new QPushButton("Paste enabled");
    m_recordState = new QLabel("Recording paused.");
    m_recordState->setAlignment(Qt::AlignCenter);

    m_enable = true;

    QGridLayout *layout = new QGridLayout;
    layout->addWidget(m_record,0,0,1,1);
    layout->addWidget(m_recordPause,1,0,1,1);
    layout->addWidget(m_enableB,2,0,1,1);
    layout->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->setOutputLocation(QUrl::fromLocalFile(m_dossier));
     m_audioRecorder->setAudioInput(m_audioRecorder->defaultAudioInput());

     QAudioEncoderSettings settings;
     settings.setCodec("audio/amr");
     settings.setSampleRate(16000);
     settings.setBitRate(32);
     settings.setQuality(QMultimedia::HighQuality);
     settings.setEncodingMode(QMultimedia::ConstantQualityEncoding);

     m_audioRecorder->setEncodingSettings(settings);

     m_audio = new QMediaPlayer;
     m_audio->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->state() == QMediaRecorder::StoppedState) {
        m_record->setText("Stop");
        m_recordPause->setEnabled(true);
        m_audioRecorder->record();
        m_recordState->setText("Recording.");
    }
     else
    {
        m_audioRecorder->stop();
        m_audio->setMedia(QUrl::fromLocalFile(m_dossier));
        emit end();
    }
}

      void MainWindow::togglePause()
{
     if (m_audioRecorder->state() != QMediaRecorder::PausedState)
    {
        m_audioRecorder->pause();
        m_recordPause->setText("Resume");
        m_recordState->setText("Recording paused.");
    }
    else
    {
        m_audioRecorder->record();
        m_recordPause->setText("Pause");
        m_recordState->setText("Recording.");
    }
}

     void MainWindow::setEnabled()
{
    if(m_enable)
    {
        m_enableB->setText("Paste disabled");
        m_enable = false;
    }
    else
    {
        m_enableB->setText("Paste enable");
        m_enable = true;
    }
}

 Main::Main()
{
    main = new MainWindow();
    QGridLayout *layout = new QGridLayout;
    layout->addWidget(main,0,0,1,1);
    setLayout(layout);
    this->setFixedSize(200,200);
    this->setWindowTitle("Notes");
    connect(main, SIGNAL(end()), this, SLOT(restart()));
}

 void Main::restart()
{
    main = new MainWindow();
}

Thank you very much for your help.

A:

Hmm, you need to understand how pointers work in C and C++. When you write this:
void restart(Object *object)
{
    object = new Object();
}

You are copying the pointer and the pointer to the newly created object will be assigned to the "internal" copied variable inside the function. What you intended to write, I think, is to change the pointer outside.
Furthermore, if you change the pointer outside, you will need to make sure that the old object is destructed.
Having said that, this is a very poor way of resetting an object. You should forget about this concept for several reasons in my opinion. First and foremost, it can be costly for performance. You basically destruct and then construct again. I would for instance suggest to cache the default if you have sufficient memory available, but after all, it is all about trade-offs, isn't it...
Should you still insist on it, here goes the code that you would need to apply in pratice:
You will need to apply the following code for that:
void restart(Object **object)
{
    delete *object;
    *object = new Object();
}

and then the call you should be like this:
restart(&object);

