Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to use Q_DISABLE_COPY( class ) without ctor?
QtWS25 Last Chance

How to use Q_DISABLE_COPY( class ) without ctor?

Scheduled Pinned Locked Moved Solved General and Desktop
qdisablecopy
4 Posts 3 Posters 5.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    c64zottel
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Out of curiosity, why don't you need any constructor ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      C 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        Out of curiosity, why don't you need any constructor ?

        C Offline
        C Offline
        c64zottel
        wrote on last edited by
        #3

        @SGaist This is just an interface in the sense of a convention for classes inheriting the interface. The ctor is simply empty, hence, the auto generate ctor would be fine too.

        VRoninV 1 Reply Last reply
        0
        • C c64zottel

          @SGaist This is just an interface in the sense of a convention for classes inheriting the interface. The ctor is simply empty, hence, the auto generate ctor would be fine too.

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4

          @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 parent explicit InputDeviceConfigurator(QObject *parent = Q_NULLPTR) : QObject(parent){}
          • if InputDeviceConfigurator does not uses signals and slots nor defines properties then lose the QObject 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

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          4

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved