Inheriting constructor from QObject based class

Resurrection picture Resurrection · Mar 31, 2015 · Viewed 8.4k times · Source

I have a class called MiscData that inherits QObject and has a member variable (a model). And then bunch of other classes that inherit MiscData and reimplement its virtual function to populate the model. So it looks like this:

class MiscData : public QObject
{
    Q_OBJECT
public:
    explicit MiscData(QObject *parent = 0);
    QAbstractItemModel &model();
private:
    virtual void loadData() = 0;
private:
    QStandardItemModel m_Model;
}

and one of the descendant looks like this:

class LogData : public MiscData
{
    Q_OBJECT
public:
    using MiscData::MiscData;
private:
    virtual void loadData() override;
}

I know that I must use an explicit constructor for MiscData because it initializes the model member variable. But I am wondering whether it is safe to use using directive in the derived class to inherit MiscData's constructor like this.

EDIT: Based on the answer it seems to be fine event to use using QObject::QObject in the MiscData too.

Answer

Simon Warta picture Simon Warta · Mar 31, 2015

Looks like what you are doing is perfectly right since C++11.

See Inheriting constructors and C++11 Object construction improvement.

Note that this is an all-or-nothing feature; either all of that base class's constructors are forwarded or none of them are.