gpt4 book ai didi

c++ - Qt:在QThread拷贝的子类中,构造函数被编译器删除

转载 作者:太空宇宙 更新时间:2023-11-04 13:18:43 25 4
gpt4 key购买 nike

我是 Qt 的新手,正在尝试在控制台应用程序中使用 QThread。

环境:Qt 创作者 3.6.1;基于Qt5.6.0(MSVC2013 32bit);建立于 2016 年 3 月 14 日;修订版 d502727b2c

我所做的是:

  1. 创建派生类QtThread继承QThread类
  2. 创建一个标准容器 vector 并初始化几个线程
  3. 使用 std::for_each 启动所有线程

这是我的问题

首先,在QtThread类中,我必须实现拷贝构造函数,否则会出现编译错误

 C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\xmemory0:657: error: C2280: 'QtThread::QtThread(const QtThread &)': attempting to reference a deleted function

我认为我必须在这里实现复制构造函数的原因是基类 QThread 具有析构函数 ~QThread()。所以编译器会将copy ctr和move ctr标记为delete。派生类不继承基类的复制/移动ctr。

与此相关的三个问题。

在主函数中,我使用了 emplace_back()。貌似编译器用的是拷贝构造函数而不是move.Why~(std::thread是move only,不能拷贝,所以QThread can be copyed对我来说有点奇怪,也可能是我做错了但是我做了没有意识到)

我不能使用关键字 default 让编译器为我生成复制 ctr,为什么

QtThread(const QtThread &in_thread) = default; // not working, still having compile error C2280

我实现的复制构造函数不好,它只是创建另一个线程并复制线程的名称,对我来说似乎不太好,但我找不到更好的解决方案。有什么建议吗?

基类QThread 没有虚析构函数。这对我来说似乎很不寻常。这意味着派生类不能隐式调用 QThread 的析构函数。或者我根本不应该继承 QThread?

这是我声明 QtThread 的代码:

#pragma once
#include <QtCore>
#include <QDebug>

#define qtout qDebug()

class QtThread : public QThread
class QtThread : public QThread
{
Q_OBJECT
public:
QtThread():QThread(nullptr){}

explicit QtThread(const QString &in_name);
// This copy constructor create a different thread with same name, bad
QtThread(const QtThread &in_thread) : QThread() { m_name = in_thread.m_name;} // have to implement copy constructor otherwise, the code will have error: C2280 compile error

//error: C2280: 'QtThread::QtThread(const QtThread &)': attempting to reference a deleted function
//QtThread(const QtThread &in_thread) = default;
void run();

QString m_name;
};

cpp文件

#include "qtthread.h"

QtThread::QtThread(const QString &in_name)
: QThread()
, m_name(in_name)
{}

void QtThread::run()
{
qtout << "QtThread" << m_name << "start to run";
for(int i = 0; i<1000; i++)
qtout << m_name << ": " << i;
}

这里是主要功能

#include <QCoreApplication>
#include "qtthread.h"
#include <vector>
#include <algorithm>

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

std::vector<QtThread> ts;

ts.emplace_back(QtThread("Tx"));
ts.emplace_back(QtThread("T5"));
ts.emplace_back(QtThread("T6"));
std::for_each(ts.begin(), ts.end(), [](QtThread &t){t.start();});

return a.exec();
}

感谢您花时间阅读这篇长篇文章并为我提供帮助:)

[编辑 1]

从主函数中删除了一些实验性代码。

感谢 hyde 和 cod_fodder 的评论。

还有一个细节我想知道。编译器要求我为 QtThread 重载复制构造函数(否则编译器会抛出错误 CC2280)。但是我在 main 函数中尝试做的是将对象移动到容器中。我知道当移动操作失败时,对象将被复制。在这种情况下,Q_Object不应该被复制,但是QtThread对象无法移动的原因是什么?

谢谢

最佳答案

博客woboq.com/blog/qthread-you-were-not-doing-so-wrong.html中说,当需要事件循环时,我们可以使用worker/controller模式,否则继承QThread就好了.

关于c++ - Qt:在QThread拷贝的子类中,构造函数被编译器删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36194770/

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