How to use Q_DISABLE_COPY( class ) without ctor?
-
I have the following pure virtual class:
class InputDeviceConfigurator : public QObject { Q_OBJECT Q_DISABLE_COPY(InputDeviceConfigurator) public: explicit InputDeviceConfigurator(QObject *parent = 0); virtual ~InputDeviceConfigurator() = 0;
And since we can not copy QObjects, I think its good habit to prohibit it all along with Q_DISABLE_COPY.
Since I have no use for the ctor I tried to remove it.And then I get the following error:
../../UserInputManagement_subdir/UserInputManagement/joystickdeviceconfigurator.cpp: In constructor ‘JoystickDeviceConfigurator::JoystickDeviceConfigurator(JoystickDevice*)’: ../../UserInputManagement_subdir/UserInputManagement/joystickdeviceconfigurator.cpp:14:50: error: no matching function for call to ‘InputDeviceConfigurator::InputDeviceConfigurator()’ d(new JoystickDeviceConfiguratorPrivate( jd )) ^ In file included from ../../../storages/third-party-install/Qt/5.8/gcc_64/include/QtCore/qnamespace.h:43:0, from ../../../storages/third-party-install/Qt/5.8/gcc_64/include/QtCore/qobjectdefs.h:48, from ../../../storages/third-party-install/Qt/5.8/gcc_64/include/QtCore/qobject.h:46, from ../../../storages/third-party-install/Qt/5.8/gcc_64/include/QtCore/QObject:1, from ../../UserInputManagement_subdir/UserInputManagement/include/joystickdeviceconfigurator.h:4, from ../../UserInputManagement_subdir/UserInputManagement/joystickdeviceconfigurator.cpp:1: ../../UserInputManagement_subdir/UserInputManagement/include/inputdeviceconfigurator.h:12:20: note: candidate: InputDeviceConfigurator::InputDeviceConfigurator(const InputDeviceConfigurator&) <deleted> Q_DISABLE_COPY(InputDeviceConfigurator) ^ ../../../storages/third-party-install/Qt/5.8/gcc_64/include/QtCore/qglobal.h:323:5: note: in definition of macro ‘Q_DISABLE_COPY’ Class(const Class &) Q_DECL_EQ_DELETE;\ ^~~~~ ../../UserInputManagement_subdir/UserInputManagement/include/inputdeviceconfigurator.h:12:20: note: candidate expects 1 argument, 0 provided Q_DISABLE_COPY(InputDeviceConfigurator) ^ ../../../storages/third-party-install/Qt/5.8/gcc_64/include/QtCore/qglobal.h:323:5: note: in definition of macro ‘Q_DISABLE_COPY’ Class(const Class &) Q_DECL_EQ_DELETE;\ ^~~~~
JoystickDeviceConfigurator is just a client of the interface.
Why is InputDeviceConfigurator's ctor so imporant to the compiler?
-
Hi,
Out of curiosity, why don't you need any constructor ?
-
@c64zottel said in How to use Q_DISABLE_COPY( class ) without ctor?:
The ctor is simply empty
It is not. in fact it calls the QObject constructor that does a lot of stuff. removing it while leaving the
Q_DISABLE_COPY
prevents the compiler from creating a default constructor (which would be wrong anyway).Now we have 2 possible solutions:
- If your
InputDeviceConfigurator
uses signals and slots or defines properties then your only choice is to explicitly declare the constructor and pass the parentexplicit InputDeviceConfigurator(QObject *parent = Q_NULLPTR) : QObject(parent){}
- if
InputDeviceConfigurator
does not uses signals and slots nor defines properties then lose theQObject
inheritance altogether and use multiple inheritance for the derived classes.
Just to make the point clear: you don't "remove the constructor". see this table
@c64zottel said in How to use Q_DISABLE_COPY( class ) without ctor?:
the auto generate ctor would be fine too.
In cases (this one is not one of them) in which the default costructor is what you want just use
=default
- If your