【发布时间】:2018-11-22 12:32:54
【问题描述】:
我读到 qt 中的信号/槽概念应该始终按值而不是引用传递参数,以确保信号/槽在线程之间完美地工作。
我现在有一段代码,它只会在信号的参数通过引用而不是值发出时编译:
#include <QObject>
class mythirdclass {
public:
mythirdclass();
};
class mysecondclass : public QObject, public mythirdclass {
public:
mysecondclass(mythirdclass third);
};
class myclass : public QObject {
Q_OBJECT
public:
myclass();
signals:
// not working
void messageReceived(mysecondclass mymessage);
// working
// void messageReceived(mysecondclass &mymessage);
};
myclass::myclass()
{
mythirdclass third;
mysecondclass msg(third);
emit messageReceived(msg);
}
mysecondclass::mysecondclass(mythirdclass third)
{
// DO stuff
}
mythirdclass::mythirdclass()
{
}
编译错误是:
..\example\main.cpp: In constructor 'myclass::myclass()':
..\example\main.cpp:28:20: error: use of deleted function 'mysecondclass::mysecondclass(const mysecondclass&)'
emit signal(second);
^
..\example\main.cpp:8:7: note: 'mysecondclass::mysecondclass(const mysecondclass&)' is implicitly deleted because the default definition would be ill-formed:
class mysecondclass : QObject, public mythirdclass {
^
基于我想为mysecondclass 编写一个复制构造函数的错误,但是经过一些尝试我现在放弃了,因为我没有做对。
所以我的问题是:
- 为什么编译失败?
- 如果由于缺少复制构造函数而失败,为什么编译器无法隐式定义?
- 在我的例子中,工作拷贝构造函数会是什么样子?
提前致谢。
【问题讨论】:
-
您的
mysecondclass不是有意继承public QObject...? ?????? -
你说得对,忘记了。
-
这是完整代码吗?
-
@JBL 这是一个最小的例子。真实世界的场景是
myclass是一个包装类,它与QCanBusDevice和QCanBusFrame接口。mythirdclass是QCanBusFrame,mysecondclass是我在应用层任何地方使用的特定消息类。
标签: c++ qt copy-constructor qt-signals