0

I'm trying to use static polymorphism instead of dynamics polymorphism with Qt signal/slot mechanism. But I get compile error. What is wrong in my code? What is workaround?

devices.h

#ifndef DEVICES_H
#define DEVICES_H

#include <QtCore>
#include <qdebug.h>
class DeviceController : public QObject

{
    Q_OBJECT
public:
    explicit DeviceController(QObject *parent = nullptr):QObject(parent){}
    virtual ~DeviceController() {}
    void doAllDevicesInit(){
        emit deviceAInitSignal();
    }

signals:
    void deviceAInitSignal();
};

template<typename T> class BaseDevice  {
public:
    void init() {
        static_cast<T*>(this)->doInit();
        qDebug() << QString("BaseDevice initialized!");
    }
};

class DeviceA : public BaseDevice<DeviceA> {
public:
    void doInit() {
        qDebug() << "DeviceA initialized!";
     }
};
#endif // DEVICES_H

main.cpp

#include "devices.h"
int main(int argc, char *argv[])
{
    Q_UNUSED(argc);Q_UNUSED(argv);

    DeviceA deviceA;
    DeviceController deviceController;

   QObject::connect(&deviceController,&DeviceController::deviceAInitSignal,
                         &deviceA, &DeviceA::init);

    deviceController.doAllDevicesInit();
    return 0;
}

Compile output

Qt5.12.2/5.12.2/gcc_64/include/QtCore/qobjectdefs_impl.h:414:94: error: invalid static_cast from type ‘QObject*’ to type ‘QtPrivate::FunctionPointer::*)()>::Object*’ {aka ‘BaseDevice*’} FuncType::template call(static_cast(this_)->function, static_cast(r), a);

mas sam
  • 21
  • 2

1 Answers1

1

Thanks to drescherjm comment, a workaround is as follow

devices.h

...

template<typename T> 
class BaseDevice: public QObject  {
//Q_OBJECT, Error: Template classes not supported by Q_OBJECT
public:
    explicit BaseDevice(QObject *parent = nullptr):QObject(parent){}
    virtual ~BaseDevice() {}
    void init() {
        static_cast<T*>(this)->doInit();
        qDebug() << QString("BaseDevice initialized!");
    }
};

class DeviceA : public BaseDevice<DeviceA> {
Q_OBJECT
public:
    explicit DeviceA(QObject *parent = nullptr):BaseDevice<DeviceA>(parent){}
    virtual ~DeviceA() {}
    void doInit() {
        qDebug() << "DeviceA initialized!";
     }
};
#endif // DEVICES_H
mas sam
  • 21
  • 2