S
S
sddvxd2018-08-21 21:09:17
C++ / C#
sddvxd, 2018-08-21 21:09:17

Why not call a constructor with a derived class as an argument?

Hello
I'm extending the QItemSelectionModel class. Here are its constructors:

explicit QItemSelectionModel(QAbstractItemModel *model = nullptr);
    explicit QItemSelectionModel(QAbstractItemModel *model, QObject *parent);

The QAbstractItemModel class is derived from QObject. I am trying to call it in a derived class:
SimpleItemSelectionModel::SimpleItemSelectionModel(QObject *parent) : QItemSelectionModel (parent)

The IDE flags this line with the error "no constructor found to initialize `QItemSelectionModel`" Does
this rule apply to constructors? When is it possible to operate with pointers to base classes while operating on objects of derivatives?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sddvxd, 2018-08-21
@sddvxd

The person left the answer, but for some reason deleted
The problem was in explicit. The compiler doesn't care if I pass the base class object

SimpleItemSelectionModel::SimpleItemSelectionModel(QObject *parent) : QItemSelectionModel (dynamic_cast<QAbstractItemModel*>(parent))

S
Stanislav Makarov, 2018-08-21
@Nipheris

When is it possible to operate with pointers to base classes while operating on objects of derivatives?

Don't you think that your situation is exactly the opposite? You are trying to pass an object of the base class QObject under the guise of an object of the derived class QAbstractItemModel.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question