6

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.

Resurrection
  • 3,916
  • 2
  • 34
  • 56
  • It is safe. `MiscData::MiscData` is public. What problem do you have? – Dmitry Sazonov Mar 31 '15 at 08:26
  • @SaZ I have no problem yet. :-) I was just wondering whether it is safe to do this when there is QObject in the inheritance tree and the Q_OBJECT macro. I did not know whether the constructor of a QObject derived class must be explicit in all derived classes (for setting properties for instance) or I can inherit it as well like I showed. – Resurrection Mar 31 '15 at 08:36
  • 1
    In your case, next ctors will be called: `QObject( nullptr )` -> `MiscData(0)` -> `LogData()`. – Dmitry Sazonov Mar 31 '15 at 08:51
  • Based on your edit: `MiscData`'s constructor will not be explicit then, since `QObject` has exactly one constructor, which is not explicit. – Simon Warta Mar 31 '15 at 10:23

2 Answers2

5

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.

Community
  • 1
  • 1
Simon Warta
  • 10,850
  • 5
  • 40
  • 78
3

You can call the base class' constructor in the initializer list.

class LogData : public MiscData
{
    Q_OBJECT
public:
    explicit LogData(QObject *parent = 0) : MiscData(parent) {};
private:
    virtual void loadData() override;
}

where MiscData's constructor should pass parent the to QObject the same way:

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

The constructor's definition can be moved into the .cpp file if you want.

using just makes stuff available and does not call anything.

Simon Warta
  • 10,850
  • 5
  • 40
  • 78
  • I do call the QObject constructor in the initializer list and I can do the same for the derived classes (calling MiscData constructor) but I wanted to avoid it in the derived classes where I usually don't need it. Hence my question whether it is safe to inherit constructor of QObject derived class or I should rather use explicit constructor in all QObject derived classes. In other words: I may not need the constructor but QObject/Q_OBJECT related stuff might! – Resurrection Mar 31 '15 at 08:39
  • 1
    So your real question is will the explicit keyword carry over when inheriting constructors? I think yes :) – paulm Mar 31 '15 at 08:41
  • I never came across this usage of `using`. See my other answer. This one only holds for pre C++11 code. – Simon Warta Mar 31 '15 at 08:59