gpt4 book ai didi

c++ - "Recreating"C++ 中的对象没有重置属性值。 (Qt)

转载 作者:行者123 更新时间:2023-11-30 03:59:57 26 4
gpt4 key购买 nike

我认为直接把代码写下来会更容易,但我想要的是“重新创建”一个对象。所以在这个简单的例子中,我有一个以 int 作为属性的类。当我使用重新创建类时object = new Object();,int保持不变,不重置。

下面是简单题的代码(看起来过于复杂,其实是有原因的):

主要.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();
}

主要.h

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

void restart(Object *object);

#endif

对象.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;
}

对象.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__) */

因此输出将是:

4

4
Program ended with exit code: 0

我们希望最后输出 2。

现在如果你想知道我为什么做这一切,那是因为我正在用 Qt 编写程序,我遇到了同样的问题,但有点复杂:

主要.cpp

#include "mainwindow.h"
#include <QApplication>

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

主窗口.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

主窗口.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();
}

非常感谢您的帮助。

最佳答案

嗯,您需要了解指针在 C 和 C++ 中的工作原理。当你这样写时:

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

您正在复制指针,指向新创建对象的指针将分配给函数内部的“内部”复制变量。我想你打算写的是在外面改变指针。

此外,如果在外部更改指针,则需要确保销毁旧对象。

话虽如此,这是一种非常糟糕的重置对象的方法。在我看来,你应该忘记这个概念有几个原因。首先也是最重要的,它可能会影响性能。您基本上是先破坏然后再构造。例如,如果您有足够的可用内存,我会建议缓存默认值,但毕竟,这都是权衡取舍,不是吗...

如果你仍然坚持,这里是你需要在实践中应用的代码:

您需要为此应用以下代码:

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

然后调用你应该是这样的:

restart(&object);

关于c++ - "Recreating"C++ 中的对象没有重置属性值。 (Qt),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26578820/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com