Multi-level inheritance with QObjects
-
Hello,
I have problem inheriting custom abstract class which inherits QObject. Long story short heres the code (simplified):class AbstractRejestration : public QObject { Q_OBJECT public: explicit AbstractRejestration(QByteArray raw_data = 0, QObject *parent = 0) : QObject(parent) { setData(raw_data); } virtual ~AbstractRejestration() { delete parameters; } virtual void setData(QByteArray raw_data); void setParameters(AbstractParameters *params); protected: virtual void parseData() = 0; AbstractParameters *parameters; QByteArray raw_data; };
class OfflineRejestration : public AbstractRejestration { public: explicit OfflineRejestration(QByteArray raw_data = 0, QObject *parent = 0) : AbstractRejestration(raw_data, parent) {}; virtual ~OfflineRejestration(); void setData(QByteArray raw_data); void print(); void clear(); private: void parseData(); };
This is sufficent enough to generate such errors:
use of deleted function 'OfflineRejestration::OfflineRejestration(const OfflineRejestration&)' use of deleted function 'AbstractRejestration::AbstractRejestration(const AbstractRejestration&)' qobject.h:461: error: 'QObject::QObject(const QObject&)' is private 'Q_DISABLE_COPY(QObject)' error: within this context 'class AbstractRejestration : public QObject' error: use of deleted function 'QObject::QObject(const QObject&)'
I would be greatfull if someone explain me what is wrong here...
-
From the looks of it you try to create a copy of a
OfflineRejestration
object somewhere in your code (by calling the copy constructor or the assignment operator). You cannot copy aQObject
because the copy constructor and the assignment operators are declared as private. The documentation gives more information about whyQObject
s cannot be copied: https://doc.qt.io/qt-5/qobject.html#no-copy-constructor-or-assignment-operatorIt's recommended that you always use the
Q_DISABLE_COPY()
macro in your own classes. See the documentation linked above.If you really need a "copy" of a
QObject
based object the common way of doing this is by adding aclone()
function to your class. Just make sure that you don't screw things up in there (eg. usually you don't want to assign/copy the parent attribute). -
@michelson
As an addition to Joel's good points, this:QByteArray raw_data = 0
doesn't make much sense to me. What's the problem with giving an empty object here, like the following:
explicit AbstractRejestration(QByteArray raw_data = QByteArray(), QObject * parent = 0) : QObject(parent) { setData(raw_data); }
You're missing the
Q_OBJECT
macro in your derived class. And finally inliningvirtual
methods is not possible, so I advise just putting the definitions in thecpp
file.
Meanwhile, what compiler are you using? I'd guess that the overload matching is somehow confused by your declarations, judging from the errors you get.PS.
It's also a good idea to useoverride
(orQ_DECL_OVERRIDE
) to make sure you're indeed overriding a virtual method andnullptr
instead of the terrible type-safety unaware0
.If you really need a "copy" of a QObject based object the common way of doing this is by adding a clone() function to your class. Just make sure that you don't screw things up in there (eg. usually you don't want to assign/copy the parent attribute).
Not that easy (that's why copying is removed in the first place) - you'd need to decide how and if, and should you duplicate signal-slot connections (beside the obvious problem with ownership). As a whole it's not a good idea to work around the disabled copy constructor in the first place.
Kind regards.